一直在使用jszip创建和下载zip文件。我可以单独下载pdf文件。但我想将这些pdf下载到zip文件夹中,然后下载zip文件。
下载pdf的代码段:
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
window.location.href = "pdf_url"
}
};
xhttp.open("GET", "pdf_url");
xhttp.send();
向其中添加zip api:
var zip = new JSZip();
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
window.location.href = "pdf_url"
zip.file("file_name", pdf_url_content);
}
};
xhttp.open("GET", "pdf_url");
xhttp.send();
创建zip:
zip.generateAsync({type:"blob"})
.then(function callback(blob) {
saveAs(blob, "example.zip");
}
pdf_url_content应该具有网址的内容。如何阅读该内容?我尝试使用jszip-utils。无法继续。任何帮助,将不胜感激。
提前致谢。
首先,将pdf压缩是没有用的,因为用户将先使用xmlhttprequest进行解压缩,然后再将其解压缩,然后再进行下载,因此他将不得不两次下载该文件。
无论如何我都回答了你的信息
你应该适当的内容类型设置为您xmlhttprequest
和responseType
到blob
。然后,你可以使用属性访问PDF的内容response
里面onreadstatechange
回调
var zip = new JSZip();
var xhttp = new XMLHttpRequest();
xhttp.open("GET", "pdf_url");
xhttp.responseType = "blob";
xhttp.setRequestHeader("Content-type", "application/pdf");
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
zip.file("file_name", xhttp.response);
zip.generateAsync({type:"blob"})
.then(function(blob) {
saveAs(blob, "example.zip");
});
}
};
xhttp.send();
请注意,您需要FileSaver.js
下载部分(saveAs
功能)。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句