return FALSE; $r = well_tag_thread__update(array('id' => $id), $update); return $r; } function well_tag_thread_find($tagid, $page, $pagesize) { $arr = well_tag_thread__find(array('tagid' => $tagid), array('id' => -1), $page, $pagesize); return $arr; } function well_tag_thread_find_by_tid($tid, $page, $pagesize) { $arr = well_tag_thread__find(array('tid' => $tid), array(), $page, $pagesize); return $arr; } ?>javascript - HTML canvas to blob to downloadable file in IE9, 10 - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - HTML canvas to blob to downloadable file in IE9, 10 - Stack Overflow

programmeradmin2浏览0评论

I'm trying to find a clean and consistent approach for downloading the contents of a canvas as an image file.

For Chrome or Firefox, I can do the following

// Convert the canvas to a base64 string
var image = canvas.toDataURL();
image = image.replace(/^data:[a-z]*;/, 'data:application/octet-stream;');

// use the base64 string as the 'href' attribute 
var download = $('<a download="' + filename + '" target="_blank" href="' + image + '">');

Since the above doesn't work in IE, I'm trying to build a Blob with the 'window.navigator.msSaveOrOpenBlob' function.

var image = canvas.toDataURL();
image = image.replace(/^data:[a-z]*;,/, '');

// Convert from base64 to an ArrayBuffer
var byteString = atob(image);
var buffer = new ArrayBuffer(byteString.length);
var intArray = new Uint8Array(buffer);
for (var i = 0; i < byteString.length; i++) {
    intArray[i] = byteString.charCodeAt(i);
}

// Use the native blob constructor
blob = new Blob([buffer], {type: "image/png"});

// Download this blob
window.navigator.msSaveOrOpenBlob(blob, "test.png");

In the example above, do I really have to convert a canvas to base64, base64 to ArrayBuffer, and finally convert from base64 to blob? (Firefox has a 'canvas.toBlob' function, but again that's not available in IE). Also, this only works in IE10, not IE9.

Does anyone have any suggestions for a solution that will work in Chrome, Safari, Firefox, IE9, and IE10?

I'm trying to find a clean and consistent approach for downloading the contents of a canvas as an image file.

For Chrome or Firefox, I can do the following

// Convert the canvas to a base64 string
var image = canvas.toDataURL();
image = image.replace(/^data:[a-z]*;/, 'data:application/octet-stream;');

// use the base64 string as the 'href' attribute 
var download = $('<a download="' + filename + '" target="_blank" href="' + image + '">');

Since the above doesn't work in IE, I'm trying to build a Blob with the 'window.navigator.msSaveOrOpenBlob' function.

var image = canvas.toDataURL();
image = image.replace(/^data:[a-z]*;,/, '');

// Convert from base64 to an ArrayBuffer
var byteString = atob(image);
var buffer = new ArrayBuffer(byteString.length);
var intArray = new Uint8Array(buffer);
for (var i = 0; i < byteString.length; i++) {
    intArray[i] = byteString.charCodeAt(i);
}

// Use the native blob constructor
blob = new Blob([buffer], {type: "image/png"});

// Download this blob
window.navigator.msSaveOrOpenBlob(blob, "test.png");

In the example above, do I really have to convert a canvas to base64, base64 to ArrayBuffer, and finally convert from base64 to blob? (Firefox has a 'canvas.toBlob' function, but again that's not available in IE). Also, this only works in IE10, not IE9.

Does anyone have any suggestions for a solution that will work in Chrome, Safari, Firefox, IE9, and IE10?

Share Improve this question edited Nov 1, 2013 at 23:46 Daniel asked Nov 1, 2013 at 22:55 DanielDaniel 1492 silver badges7 bronze badges 2
  • BTW you do not need to strip the header of the data-uri - just set it directly for href example.IE 9 & 10 do not support the download attribute (surprise!) so there is no easy way around that except from using the server to serve the file. – user1693593 Commented Nov 1, 2013 at 23:07
  • I just added working code for IE10, but I'm still struggling to find something that works in IE9. – Daniel Commented Nov 1, 2013 at 23:49
Add a ment  | 

1 Answer 1

Reset to default 5

Yes you have to do all the extra footwork.

toBlob support in Chrome and FF is very recent, even though its been in the spec for several years. Recent enough that it wasn't even on the radar when MS made IE9 and 10.

Unfortunately MS has no intent of changing IE9 or IE10 canvas implementations, which means they will exist as they are for all eternity, with bugs and missing pieces. (IE10 canvas has several bugs that IE9 does not, that are fixed again in IE11. Like this gem. It's a real shamble.)

发布评论

评论列表(0)

  1. 暂无评论