[英]How to zip an img from file input with JSZIP
I try to zip a file from a file input element, but it does not work.我尝试从文件输入元素 zip 一个文件,但它不起作用。 How can I do it by using Javascript?
如何使用 Javascript 来做到这一点?
var input = document.getElementById("id1"); const filereader = new FileReader(); filereader.onload = function() { zip.file("picture.png", filereader.result, {binary: true}); } filereader.readAsDataURL(input.files[0]);
<input type="file" id="id1" style="border-radius: 0.1px; width: 40%;" accept="image/*">
As zip.file(name, data [,options])
accept blob
datatype, you just need to get it's files由于
zip.file(name, data [,options])
接受blob
数据类型,您只需要获取它的文件
document.getElementById("id1").addEventListener("change", function (params) {
var zip = new JSZip();
var temp = this.files[0];
zip.file(temp.name, temp);
zip.generateAsync({ type: "blob" })
.then(function (content) {
// see FileSaver.js
saveAs(content, "example.zip");
});
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.