html src data:image,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:

2wBDAAMCAgICAgM....%5C%22

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?

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!

回答1:

Simply replace image/jpeg with application/octet-stream. The client would not recognise the URL as an inline-able resource, and prompt a download dialog.

A simple JavaScript solution would be:

//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 element,

Another method is to use a 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

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'); //

}

// Use the URL object to create a temporary URL

var url = (window.webkitURL || window.URL).createObjectURL(blob);

location.href = url; //

};

Relevant documentation

atob

Typed arrays

URL.createObjectURL

Blob and BlobBuilder

回答2:

you can use the download attribute on an a tag ...

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

回答3:

I guess an img tag is needed as a child of an a tag, the following way:

or

OtherFile.jpg

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.

From JavaScript it could be generated like this:

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

回答4:

Take a look at FileSaver.js. It provides a handy saveAs function which takes care of most browser specific quirks.

来源:https://stackoverflow.com/questions/10473932/browser-html-force-download-of-image-from-src-dataimage-jpegbase64

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值