I have a canvas in HTML5 that can end up being a very long dataURL (long enough to crash chrome if they try to navigate to it). Because of that, I'm trying to save the image in a zip using JSZip. I've tried the following two things:
var zip = new JSZip();
var savable = new Image();
savable.src = canvas.toDataURL();
zip.file("image.png", savable, {base64: true});
var content = zip.generate();
var blobLink = document.getElementById('blob');
blobLink.download = "image.zip";
blobLink.href = window.URL.createObjectURL(zip.generate({type:"blob"}));
This causes the following error:
Uncaught TypeError: Object #<HTMLImageElement> has no method 'replace'
I also tried this:
var zip = new JSZip();
zip.file("image.png", canvas.toDataURL(), {base64: true});
var content = zip.generate();
var blobLink = document.getElementById('blob');
blobLink.download = "image.zip";
blobLink.href = window.URL.createObjectURL(zip.generate({type:"blob"}));
That appears to work, but then the image in the zip is not valid. What can I do to save the image properly?
I have a canvas in HTML5 that can end up being a very long dataURL (long enough to crash chrome if they try to navigate to it). Because of that, I'm trying to save the image in a zip using JSZip. I've tried the following two things:
var zip = new JSZip();
var savable = new Image();
savable.src = canvas.toDataURL();
zip.file("image.png", savable, {base64: true});
var content = zip.generate();
var blobLink = document.getElementById('blob');
blobLink.download = "image.zip";
blobLink.href = window.URL.createObjectURL(zip.generate({type:"blob"}));
This causes the following error:
Uncaught TypeError: Object #<HTMLImageElement> has no method 'replace'
I also tried this:
var zip = new JSZip();
zip.file("image.png", canvas.toDataURL(), {base64: true});
var content = zip.generate();
var blobLink = document.getElementById('blob');
blobLink.download = "image.zip";
blobLink.href = window.URL.createObjectURL(zip.generate({type:"blob"}));
That appears to work, but then the image in the zip is not valid. What can I do to save the image properly?
Share Improve this question asked Mar 8, 2013 at 5:33 FibericonFibericon 5,79313 gold badges39 silver badges65 bronze badges1 Answer
Reset to default 21You're generating a data uri, which has schema, mime-type etc before the actual base64 data data:[<MIME-type>][;charset=<encoding>][;base64],<data>
. You'll have to remove all the content before the data then use it.
zip.file("image.png", savable.src.substr(savable.src.indexOf(',')+1), {base64: true});