在新选项卡中打开Base64

时间:2015-12-17 16:27:24

标签: javascript html5 base64

我有一个Base64编码的文档,可以是PDF文件或图像。 我想在HTML5页面中创建一个按钮,在新标签页中打开这个base64(或者新页面无关紧要)

我发现这段代码可以解决问题:

<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>

效果很好。 但是当我用

替换http链接时
href="data:application/octet-stream;base64,/9j/4A.."

然后下载文件但不在浏览器中显示。

我该怎么做?

由于

4 个答案:

答案 0 :(得分:10)

您的MIME类型设置为'application / octet-stream',通常会下载并不显示(取决于浏览器和系统设置)。如果您尝试加载浏览器可显示的内容,那么您应该为您的内容使用更合适的MIME类型,以便它显示内联并且不会被下载。

请注意:As of Chrome 60upcoming version of FireFox(虽然它显示为FireFox will still support images),但无法在浏览器的顶级框架中打开数据URI({{3} }),但它们可以在iframe和img元素中打开。

下面的解决方法已经过测试,可以在上述浏览器的最新版本中使用。这也可以用img标签重写以显示图像。

<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);
});

但是,如果您打算下载内容(与问题中的陈述意图相反,但一般适用于'application / octet-stream'内容),这应该足够了:

<a href='data:application/octet-stream;base64,SWYgSSBoYWQgYSBuaWNrbGUgZm9yIGV2ZXJ5IHRpbWUgSSBoYWQgYSBuaWNrbGUsIEknZCBoYXZlIGVhdGVuIHR3aWNlIGFzIG1hbnkgcGlja2xlcy4=' download>Download this</a>

答案 1 :(得分:4)

在chrome和Firefox上测试,只需将base64值添加到href属性中即可(无onclick&am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LEJKX2EEkomBAUCxRi42VDADxyTYDVogV+wSChqmKxEKCDAYFDFj4OmwbY7bDGdBhtrnTQYOigeChUmc1K3QTnAUfEgGFgAWt88hKA6aCRIXhxnQ1yg3BCayK44EWdkUQcBByEQChFXfCB776aQsG0BIlQgQgE8qO26X1h8cEUep8ngRBnOy74E9QgRgEAC8SvOfQkh7FDBDmS43PmGoIiKUUEGkMEC/PJHgxw0xH74yx/3XnaYRJgMB8obxQW6kL9QYEJ0FIFgByfIL7/IQAlvQwEpnAC7DtLNJCKUoO/w45c44GwCXiAFB/OXAATQryUxdN4LfFiwgjCNYg+kYMIEFkCKDs6PKAIJouyGWMS1FSKJOMRB/BoIxYJIUXFUxNwoIkEKPAgCBZSQHQ1A2EWDfDEUVLyADj5AChSIQW6gu10bE/JG2VnCZGfo4R4d0sdQoBAHhPjhIB94v/wRoRKQWGRHgrhGSQJxCS+0pCZbEhAAOw==">test</a>
</body>

</html>
&#13;
&#13;
&#13;

答案 2 :(得分:0)

我正在根据亚伦的答案在我的问题中添加答案:

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);
});

答案 3 :(得分: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>