js多文件下载并重命名
let list=['...']
function downloadFile(list,index){//下载文件方法
if(index>=list.length){
return
}
let url=list[index];
const x = new window.XMLHttpRequest();
x.open('GET', url, true);
x.responseType = 'blob';
var fileName = url.substring(url.lastIndexOf("."),url.length);
x.onload = () => {
const url = window.URL.createObjectURL(x.response);
const a = document.createElement('a');
a.href = url;
a.download = "每日意见反馈表"+(index+1)+fileName;
a.click();
downloadFile(list,index+1)
};
x.send();
}
downloadFile(list,0)
js多文件重命名打包Zip压缩包并下载此压缩包
// <script src="https://cdn.bootcdn.net/ajax/libs/jszip/3.6.0/jszip.min.js"></script>
// <script src="https://unpkg.com/axios/dist/axios.min.js"></script>
let list=[{
namne:'文件名',
url:'...'
}]
async function downloadFileZip(list,zipName){
var zip = new JSZip();
for (let i = 0; i < list.length; i++) {
let {data} = await axios({
method: "get",
url: list[i].url,
responseType: "blob",
});
zip.file(list[i].name, data);
}
zip.generateAsync({type: "blob"}).then(function (content) {
let url = window.URL.createObjectURL(content);
downloadFile(url, zipName);
});
}
downloadFileZip(list,'压缩包名')
js多文件重命名分文件夹打包Zip压缩包并下载此压缩包
var zip = new JSZip();
for (let i = 0; i < res.data.length; i++) {
let stuObj = res.data[i]
let rName = stuObj.realName
let list=[{
namne:'文件名',
url:'...'
}]
let folder = zip.folder(rName);
for (let j = 0; j < list.length; j++) {
let {data} = await axios({
method: "get",
url: list[j].url,
responseType: "blob",
});
folder.file(rName + '-' + list[j].name, data);
}
}
zip.generateAsync({type: "blob"}).then(function (content) {
let url = window.URL.createObjectURL(content);
downloadFile(url, $("#classesId").find('option:selected').text());
});