37 lines
1.3 KiB
JavaScript
37 lines
1.3 KiB
JavaScript
import streamSaver from "streamsaver";
|
|
import "streamsaver/examples/zip-stream.js"
|
|
export const downloadImage = (zipName, files) => {
|
|
return new Promise((resolve) => {
|
|
console.log("同步下载打包开始时间:" + new Date());
|
|
// 创建压缩文件输出流
|
|
const zipFileOutputStream = streamSaver.createWriteStream(zipName);
|
|
// 创建下载文件流
|
|
const fileIterator = files.values();
|
|
const readableZipStream = new window.ZIP({
|
|
async pull(ctrl) {
|
|
const fileInfo = fileIterator.next();
|
|
if (fileInfo.done) {//迭代终止
|
|
ctrl.close();
|
|
} else {
|
|
const { name, url } = fileInfo.value;
|
|
return fetch(url).then(res => {
|
|
ctrl.enqueue({
|
|
name,
|
|
stream: () => res.body
|
|
})
|
|
})
|
|
}
|
|
}
|
|
});
|
|
if (window.WritableStream && readableZipStream.pipeTo) {
|
|
// 开始下载
|
|
readableZipStream
|
|
.pipeTo(zipFileOutputStream)
|
|
.then(() => { console.log("同步下载打包结束时间:" + new Date()); resolve(true) })
|
|
} else {
|
|
resolve(false);
|
|
}
|
|
})
|
|
|
|
};
|