I'm tring to add a JPG image I created with html2canvas to a Zip file using JSZip. When I save the raw JPG file, it comes out fine. But when I zip it, the image comes out corrupt (when I open the image file, no data appears). Here is my script:
<!DOCTYPE html>
<html>
<head>
<script src="https://stuk.github.io/jszip/dist/jszip.js"></script>
<script src="http://html2canvas.hertzen.com/dist/html2canvas.min.js"></script>
<script type="text/javascript" src="FileSaver.min.js"></script>
</head>
<body>
<div id="capture" style="padding: 0; background: #f5da55; width: 400px; height: 200px;">
<h4 style="color: #000; ">Hello, world!</h4>
</div>
<script type="text/javascript">
html2canvas( document.querySelector("#capture") ).then(canvas => {
var img = new Image(); img.src = canvas.toDataURL( 'image/jpeg' );
// * * * This JPG is good:
saveAs( img.src, 'file1.jpg');
var zip = new JSZip();
// Add a file to the zip file, in this case an image with data URI as contents
// * * * This JPG comes out corrupted:
zip.file( 'file2.jpg', img.src, {binary: true} );
// Generate the zip file asynchronously
zip.generateAsync({type:"blob"}).then(function(content) {
saveAs( content, 'archive.zip' );
});
});
</script>
</body>
</html>
Please help me add the data URI image to the zip file. Many thanks!