我正在尝试使用JSZip将使用html2canvas创建的JPG图像添加到Zip文件中。当我保存原始JPG文件时,结果很好。但是,当我压缩它时,图像会损坏(当我打开图像文件时,没有数据出现)。这是我的脚本:
<!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>
请帮助我将数据URI图像添加到zip文件中。非常感谢!
米脂
墨色风雨
相关分类