Trying to save canvas PNG data url to disk with HTML5 filesystem, but when I retrieve as URL, it's invalid

半城伤御伤魂 提交于 2019-12-02 18:32:33

data is a string, so when you pass it to blob, the binary data will be that string in UTF-8 encoding. You want binary data not a string.

You can do it like:

var canvas = document.createElement("canvas");


var dataURL = canvas.toDataURL( "image/png" );
var data = atob( dataURL.substring( "data:image/png;base64,".length ) ),
    asArray = new Uint8Array(data.length);

for( var i = 0, len = data.length; i < len; ++i ) {
    asArray[i] = data.charCodeAt(i);    
}

var blob = new Blob( [ asArray.buffer ], {type: "image/png"} );

There is also canvas.toBlob available in future but not currently in Chrome.

Demo http://jsfiddle.net/GaLRS/

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!