import JSZip from '../js/lib/jszip.min'
import axios from '../js/lib/axios.min'
import {saveAs} from '../js/lib/FileSaver'
{
let that = this
const fileData = [];
const zip = new JSZip();
const cache = {};
const promises = [];
if(that.transferList.length === 0) {
that.$message({
type: 'warning',
message: '请选择'
})
return
}
<!--当前需要处理的文件-->
this.fileList.forEach(item => {
if(item.picafter !== '') {
fileData.push(item)
}
})
const getFile = url => {
return new Promise((resolve, reject) => {
axios({
method: 'get',
url: url,
responseType: 'arraybuffer'
}).then(data => {
/*返回二进制流文件*/
resolve(data.data)
}).catch(error => {
reject(error.toString())
})
})
};
fileData.forEach((item, i) => {
const promise = getFile(item.picafter).then((data) => { // 下载文件, 并存成ArrayBuffer对象
let file_name = (item.name ? item.name : i) + '.' + item.type; // 获取文件名
zip.file(file_name, data, {binary: true}); // 逐个添加文件
cache[file_name] = data
});
promises.push(promise);
});
Promise.all(promises).then(() => {
console.log(promises)
zip.generateAsync({
type: "blob"
}).then(content => {
console.log(content)
that.$message({
message: `下载成功`,
type: "success"
});
// 生成二进制流
saveAs(content, "风格迁移文件.zip"); // 利用file-saver保存文件
});
});
}
网友评论