12

I have a Base64 encoded document which can be PDF file or image. I would like to create a button in HTML5 page that opens this base64 in a new tab (or new page it does not matter)

I found this code can do the trick :

<a href="http://chriscoyier.net" 
   onclick="window.open(this.href); return false;" 
   onkeypress="window.open(this.href); return false;">
     This link will open in new window/tab
</a>

It works well. But when I replace the http link with

href="data:application/octet-stream;base64,/9j/4A.."

then the file is downloaded but not displayed in browser.

Nakilon
  • 34,866
  • 14
  • 107
  • 142
user3249592
  • 383
  • 2
  • 3
  • 13

4 Answers4

18

Your MIME type is set to 'application/octet-stream', which will generally be downloaded and not displayed (depending on browser and system settings). If you are trying to load browser-displayable content, then you should use a more appropriate MIME type for your content so that it displays inline and does not get downloaded.

Please note: As of Chrome 60 and an upcoming version of FireFox (although it appears FireFox will still support images), data URIs cannot be opened in the top-level frame of the browser (and it was never supported in IE/Edge), but they can be opened in iframes and img elements.

The workaround below is tested and working in the latest version of the above browsers. This could also be rewritten with an img tag to display an image.

<button id='btnDownload'>Download</button>
document.getElementById('btnDownload').addEventListener('click', function(){
    var w = window.open('about:blank');

    setTimeout(function(){ //FireFox seems to require a setTimeout for this to work.
        w.document.body.appendChild(w.document.createElement('iframe'))
            .src = 'data:application/octet-stream;base64,SWYgSSBoYWQgYSBuaWNrbGUgZm9yIGV2ZXJ5IHRpbWUgSSBoYWQgYSBuaWNrbGUsIEknZCBoYXZlIGVhdGVuIHR3aWNlIGFzIG1hbnkgcGlja2xlcy4=';
    }, 0);
});

If, however, your intention is to just download the content (contrary to the stated intent in the question, but applicable to 'application/octet-stream' content in general), this should suffice:

<a href='data:application/octet-stream;base64,SWYgSSBoYWQgYSBuaWNrbGUgZm9yIGV2ZXJ5IHRpbWUgSSBoYWQgYSBuaWNrbGUsIEknZCBoYXZlIGVhdGVuIHR3aWNlIGFzIG1hbnkgcGlja2xlcy4=' download>Download this</a>
Aaron J Spetner
  • 2,117
  • 1
  • 18
  • 30
8

Tested on chrome and Firefox, just add the base64 value into the href attribute works (without onclick & onkeypress events). Just like that :

<html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+wSChqmKxEKCDAYFDFj4OmwbY7bDGdBhtrnTQYOigeChUmc1K3QTnAUfEgGFgAWt88hKA6aCRIXhxnQ1yg3BCayK44EWdkUQcBByEQChFXfCB776aQsG0BIlQgQgE8qO26X1h8cEUep8ngRBnOy74E9QgRgEAC8SvOfQkh7FDBDmS43PmGoIiKUUEGkMEC/PJHgxw0xH74yx/3XnaYRJgMB8obxQW6kL9QYEJ0FIFgByfIL7/IQAlvQwEpnAC7DtLNJCKUoO/w45c44GwCXiAFB/OXAATQryUxdN4LfFiwgjCNYg+kYMIEFkCKDs6PKAIJouyGWMS1FSKJOMRB/BoIxYJIUXFUxNwoIkEKPAgCBZSQHQ1A2EWDfDEUVLyADj5AChSIQW6gu10bE/JG2VnCZGfo4R4d0sdQoBAHhPjhIB94v/wRoRKQWGRHgrhGSQJxCS+0pCZbEhAAOw==">test</a>
</body>

</html>
halfzebra
  • 6,771
  • 4
  • 32
  • 47
  • Thank you for your answer. I've tested what I have on iPad on chrome and safari and it works weel. It seems it did not work on my computer because of web browser preferences. Maybe. By the way if it can help you, I used "data:application/octet-stream;base64" to ignore what kind of file my base64 is. – user3249592 Dec 17 '15 at 16:53
  • This solution does not work for IE.Any alternative solution to make this work in IE? – Siddharth_Vyas Jul 06 '17 at 05:47
  • 4
    This no longer works in Chrome and will stop working in Firefox as well. See my answer for more details. – Aaron J Spetner Oct 01 '17 at 08:59
  • I just ran that code snippet in chrome and I see a little red bus – jcollum Feb 28 '19 at 23:58
  • 2
    chrome display error in console: Not allowed to navigate top frame to data URL: – Uttam Panara Mar 01 '19 at 12:53
1

I am adding the answer to my question based on Aaron's answer:

document.getElementById('btnDownload').addEventListener('click', function(){
    var w = window.open('about:blank');
    setTimeout(function(){ //FireFox seems to require a setTimeout for this to 
    work.

        w.document.body.appendChild(w.document.createElement('iframe'))
            .src = $this.attr('href');
        w.document.getElementsByTagName("iframe")[0].style.width = '100%';
        w.document.getElementsByTagName("iframe")[0].style.height = '100%';
    }, 0);
});
Karl Taylor
  • 4,839
  • 3
  • 34
  • 62
richardwhitney
  • 506
  • 1
  • 6
  • 21
0
    <a href="aHR0cHM6Ly93d3cuc3dpdGNoLXRvcnJlbnRzLmNvbS9kb3dubG9hZC80MTMvU3VwZXIlMjBTbWFzaCUyMEJyb3MlMjBVbHRpbWF0ZS50b3JyZW50" 
     onclick="window.open(this.href); return false;" 
     onkeypress="window.open(this.href); return false;">
       This link will open in new window/tab
    </a>
Ed Lucas
  • 5,955
  • 4
  • 30
  • 42