简体   繁体   English

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

[英]Browser/HTML Force download of image from src=“data:image/jpeg;base64…”

I am generating a image on client side and I display it with HTML like this: 我在客户端生成图像,并使用HTML显示如下:

<img src="data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAMCAgICAgM...."/>

I want to offer the possibility to download the generated Image. 我想提供下载生成的图像的可能性。

How can I realize that the browser is opening a file save dialoge (or just download the image like chrome or firefox to the download folder would do) which allows the user to save the image without doing right click and save as on the image? 我怎么能知道浏览器正在打开文件保存对话框(或者只是将chrome或firefox这样的图像下载到下载文件夹中即可),从而允许用户保存图像而无需右键单击并另存为图像?

I would prefer a solution without server interaction. 我希望没有服务器交互的解决方案。 So I am aware that it would be possible if I first upload the Image and then start the download. 因此,我知道如果我先上传图像然后开始下载,那将是可能的。

Thanks a lot! 非常感谢!

Simply replace image/jpeg with application/octet-stream . 只需将image/jpeg替换为application/octet-stream The client would not recognise the URL as an inline-able resource, and prompt a download dialog. 客户端不会将该URL识别为可内联资源,并提示下载对话框。

A simple JavaScript solution would be: 一个简单的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, 

Another method is to use a blob: URI: 另一种方法是使用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!
};

Relevant documentation 相关文件

you can use the download attribute on an a tag ... 您可以在标签上使用download属性...

<a href="data:image/jpeg;base64,/9j/4AAQSkZ..." download="filename.jpg"></a>

see more: https://developer.mozilla.org/en/HTML/element/a#attr-download 查看更多: https : //developer.mozilla.org/en/HTML/element/a#attr-download

I guess an img tag is needed as a child of an a tag, the following way: 我猜img标签需要作为一个标签,通过以下方式的一个孩子:

<a download="YourFileName.jpeg" href="data:image/jpeg;base64,iVBO...CYII=">
    <img src="data:image/jpeg;base64,iVBO...CYII="></img>
</a>

or 要么

<a download="YourFileName.jpeg" href="/path/to/OtherFile.jpg">
    <img src="/path/to/OtherFile.jpg"></img>
</a>

Only using the a tag as explained in #15 didn't worked for me with the latest version of Firefox and Chrome, but putting the same image data in both a.href and img.src tags worked for me. 仅使用#15中说明的a标记不适用于最新版本的Firefox和Chrome,但将相同的图像数据放在a.hrefimg.src标记中对我却有效。

From JavaScript it could be generated like this: 从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);

Take a look at FileSaver.js . 看一下FileSaver.js It provides a handy saveAs function which takes care of most browser specific quirks. 它提供了一个方便的saveAs函数,可以处理大多数特定于浏览器的怪癖。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM