在IE中保存画布本地 [英] saving canvas locally in IE
问题描述
我想在IE中保存画布。
Hi I want to save a canvas locally in IE.
var img = canvas.toDataURL("image/png").replace("image/png", "image/octet-stream");
我无法通过以下方式下载它。
I couldn't manage to download it with following ways.
1) document.execCommand("SaveAs"..
2) window.location.href = img;
3) $.fileDownload(img); // jquery download file library-
4) canvas2image // cross domain problem.
有没有办法在IE中保存画布,没有base64或跨域问题?非常感谢你。
Is there a way to save canvas locally in IE without base64 or cross domain problem? Thank you very much.
推荐答案
我知道这是晚了,但是我在搜索中偶然发现这个问题,做同样的事情我想分享一个适用于我的解决方案。
I know this is late, but I stumbled on this question in my search to do the same thing and I wanted to share a solution that's working for me.
假设你已经有一个数据URI,你可以创建一个 blob ,然后使用 msSaveBlob 或 msSaveOrOpenBlob
Assuming you have a data URI already, you can create a blob and then use msSaveBlob or msSaveOrOpenBlob
示例:
dataURItoBlob = function(dataURI) {
var binary = atob(dataURI.split(',')[1]);
var array = [];
for(var i = 0; i < binary.length; i++) {
array.push(binary.charCodeAt(i));
}
return new Blob([new Uint8Array(array)], {type: 'image/png'});
}
var blob = dataURItoBlob(uri);
window.navigator.msSaveOrOpenBlob(blob, "my-image.png");
我使用这个 answer 我的一大堆解决方案。
I used this answer for a bulk of my solution.
输入后,我意识到这并没有真正帮助您解决跨域问题,所以它是最好的部分答案。所以我可以说,如果跨域是一个问题,可以考虑使用数据URI。
After typing this, I realize this doesn't really help you with the cross domain issue, so it's a partial answer at best. With that, all I can say is consider using data URIs where possible, if cross domain is an issue.
这篇关于在IE中保存画布本地的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!