admin管理员组文章数量:1193775
I get the base64-encoded image form the canvas as:
var dataURL = canvas.toDataURL( "image/png" );
Then I turn it into data like this:
//Remove the beginning identifier and use Chrome/Firefox?safari built int base64Decoder
var data = atob( dataURL.substring( "data:image/png;base64,".length ) );
Then I write it to the filesystem via:
event.createWriter(
function(writerEvent)
{
//The success handler
writerEvent.onwriteend = function(finishEvent)
{
...
};
//Error handler
writerEvent.onerror = settings.error;
// Create a new Blob
var blob = new Blob( [ data ], { type: "image/png" } );
//Write it into the path
writerEvent.write( blob );
}
}
I try to set it as src of an image like this:
document.getElementById( "saved" ).src = event.toURL();
That writes the file and I am able to find it and get a url (by reading it and using the event: event.toURL()
. But the image shows as a broken image icon on the web page. What am I doing wrong?
I get the base64-encoded image form the canvas as:
var dataURL = canvas.toDataURL( "image/png" );
Then I turn it into data like this:
//Remove the beginning identifier and use Chrome/Firefox?safari built int base64Decoder
var data = atob( dataURL.substring( "data:image/png;base64,".length ) );
Then I write it to the filesystem via:
event.createWriter(
function(writerEvent)
{
//The success handler
writerEvent.onwriteend = function(finishEvent)
{
...
};
//Error handler
writerEvent.onerror = settings.error;
// Create a new Blob
var blob = new Blob( [ data ], { type: "image/png" } );
//Write it into the path
writerEvent.write( blob );
}
}
I try to set it as src of an image like this:
document.getElementById( "saved" ).src = event.toURL();
That writes the file and I am able to find it and get a url (by reading it and using the event: event.toURL()
. But the image shows as a broken image icon on the web page. What am I doing wrong?
1 Answer
Reset to default 26data
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/
本文标签:
版权声明:本文标题:javascript - Trying to save canvas PNG data url to disk with HTML5 filesystem, but when I retrieve as URL, it's invalid 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738463329a2088156.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
event
object? Can you show the code you're using to read the file? – MaxArt Commented Jun 26, 2013 at 23:45