浏览器/ HTML强制从src ="data:image/jpeg; base64 ..."下载图像

ale*_*lex 81 html javascript browser html5 image

我在客户端生成一个图像,我用这样的HTML显示它:

<img src="data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAMCAgICAgM...."/>
Run Code Online (Sandbox Code Playgroud)

我想提供下载生成的图像的可能性.

我如何才能意识到浏览器正在打开文件保存dialoge(或者只是将图像像chrome或firefox一样下载到下载文件夹中),这样用户可以保存图像而无需右键单击并另存为图像?

我更喜欢没有服务器交互的解决方案.所以我知道如果我首先上传Image然后开始下载就可以了.

非常感谢!

Rob*_*b W 113

只需更换image/jpegapplication/octet-stream.客户端不会将URL识别为内联资源,并提示下载对话框.

一个简单的JavaScript解决方案是:

//var img = reference to image
var url = img.src.replace(/^data:image\/[^;]+/, 'data:application/octet-stream');
window.open(url);
// Or perhaps: location.href = url;
// Or even setting the location of an <iframe> element, 
Run Code Online (Sandbox Code Playgroud)

另一种方法是使用blob:URI:

var img = document.images[0];
img.onclick = function() {
    // atob to base64_decode the data-URI
    var image_data = atob(img.src.split(',')[1]);
    // Use typed arrays to convert the binary data to a Blob
    var arraybuffer = new ArrayBuffer(image_data.length);
    var view = new Uint8Array(arraybuffer);
    for (var i=0; i<image_data.length; i++) {
        view[i] = image_data.charCodeAt(i) & 0xff;
    }
    try {
        // This is the recommended method:
        var blob = new Blob([arraybuffer], {type: 'application/octet-stream'});
    } catch (e) {
        // The BlobBuilder API has been deprecated in favour of Blob, but older
        // browsers don't know about the Blob constructor
        // IE10 also supports BlobBuilder, but since the `Blob` constructor
        //  also works, there's no need to add `MSBlobBuilder`.
        var bb = new (window.WebKitBlobBuilder || window.MozBlobBuilder);
        bb.append(arraybuffer);
        var blob = bb.getBlob('application/octet-stream'); // <-- Here's the Blob
    }

    // Use the URL object to create a temporary URL
    var url = (window.webkitURL || window.URL).createObjectURL(blob);
    location.href = url; // <-- Download!
};
Run Code Online (Sandbox Code Playgroud)

相关文件

  • 你认为第一种方法会被弃用,因为它不再适用于chrome 19吗? (2认同)

Bru*_*dge 91

你可以在一个标签上使用download属性......

<a href="data:image/jpeg;base64,/9j/4AAQSkZ..." download="filename.jpg"></a>
Run Code Online (Sandbox Code Playgroud)

了解更多信息:https://developer.mozilla.org/en/HTML/element/a#attr-download

  • 我知道这是一个老帖子但是根据W3Schools网站的'下载'属性不支持IE,Safari和Opera v.<12 http://www.w3schools.com/tags/tryit.asp?filename=tryhtml5_a_download2其实我尝试用IE浏览器,它不起作用.... :( (5认同)
  • 在评论时,仍然不支持Safari和IE的`download`属性. (5认同)
  • 它有 base64 长度限制,您无法使用此方法下载更大的图像。 (2认同)

小智 14

我猜的IMG标签需要作为一个孩子一个标签,方式如下:

<a download="YourFileName.jpeg" href="data:image/jpeg;base64,iVBO...CYII=">
    <img src="data:image/jpeg;base64,iVBO...CYII="></img>
</a>
Run Code Online (Sandbox Code Playgroud)

要么

<a download="YourFileName.jpeg" href="/path/to/OtherFile.jpg">
    <img src="/path/to/OtherFile.jpg"></img>
</a>
Run Code Online (Sandbox Code Playgroud)

只使用#15中解释的a标签对我来说不适用于最新版本的Firefox和Chrome,但是在a.hrefimg.src标签中放置相同的图像数据对我有用.

从JavaScript可以像这样生成:

var data = canvas.toDataURL("image/jpeg");

var img = document.createElement('img');
img.src = data;

var a = document.createElement('a');
a.setAttribute("download", "YourFileName.jpeg");
a.setAttribute("href", data);
a.appendChild(img);

var w = open();
w.document.title = 'Export Image';
w.document.body.innerHTML = 'Left-click on the image to save it.';
w.document.body.appendChild(a);
Run Code Online (Sandbox Code Playgroud)


jes*_*sal 5

看看FileSaver.js.它提供了一个方便的saveAs功能,可以处理大多数浏览器特定的怪癖.