feat: 附件下载封装

This commit is contained in:
kun 2023-08-26 17:15:23 +08:00
parent d7a47c544d
commit 46064646c9
4 changed files with 49 additions and 105 deletions

43
src/utils/annexDowload.ts Normal file
View File

@ -0,0 +1,43 @@
import axios from "axios";
import JSZip from "jszip";
import FileSaver from "file-saver";
/**
* @description
* */
const getFile = (url: any) => {
return new Promise((resolve: any, reject: any) => {
axios({
method: "get",
url,
responseType: "blob"
})
.then((res: any) => {
resolve(res.data);
})
.catch(error => {
reject(error.toString());
});
});
};
const DownLoad = (arrList: any) => {
const arr = arrList;
const zip = new JSZip();
const cache = {};
const promises = [];
arr.forEach((item, i) => {
const promise = getFile(item.fileUrl).then(data => {
const file_name = i + 1 + item.fileName;
zip.file(file_name, data, { binary: true });
cache[file_name] = data;
});
promises.push(promise);
});
Promise.all(promises).then(() => {
zip.generateAsync({ type: "blob" }).then(content => {
FileSaver.saveAs(content, "附件.zip");
});
});
};
export default DownLoad;

View File

@ -282,9 +282,7 @@ import { getDicList } from "@/api/modules/jxjview";
import { singleEngineer } from "@/api/modules/common"; import { singleEngineer } from "@/api/modules/common";
import FilesUploadPlus from "@/components/FilesUploadPlus/FilesUpload.vue"; import FilesUploadPlus from "@/components/FilesUploadPlus/FilesUpload.vue";
import transformInfo from "./transformInfo.vue"; import transformInfo from "./transformInfo.vue";
import axios from "axios"; import DownLoad from "@/utils/annexDowload";
import JSZip from "jszip";
import FileSaver from "file-saver";
const current = ref({ const current = ref({
files: [] files: []
}); });
@ -334,42 +332,11 @@ const validatorPhone = (e: any, index: any) => {
recordData.value[index].headPersonPhone = ""; recordData.value[index].headPersonPhone = "";
} }
}; };
const getFile = (url: any) => {
return new Promise((resolve: any, reject: any) => {
axios({
method: "get",
url,
responseType: "blob"
})
.then((res: any) => {
resolve(res.data);
})
.catch(error => {
reject(error.toString());
});
});
};
// //
const onDownLoad = (row: any) => { const onDownLoad = (row: any) => {
console.log(row); console.log(row);
if (row.fileList && row.fileList.length > 0) { if (row.fileList && row.fileList.length > 0) {
const arr = row.fileList; DownLoad(row.fileList);
const zip = new JSZip();
const cache = {};
const promises = [];
arr.forEach((item, i) => {
const promise = getFile(item.fileUrl).then(data => {
const file_name = i + 1 + item.fileName;
zip.file(file_name, data, { binary: true });
cache[file_name] = data;
});
promises.push(promise);
});
Promise.all(promises).then(() => {
zip.generateAsync({ type: "blob" }).then(content => {
FileSaver.saveAs(content, "附件.zip");
});
});
} else { } else {
ElMessage.error("暂无可下载文件"); ElMessage.error("暂无可下载文件");
} }

View File

@ -269,9 +269,7 @@ import { getDicList } from "@/api/modules/jxjview";
import { singleEngineer } from "@/api/modules/common"; import { singleEngineer } from "@/api/modules/common";
import FilesUploadPlus from "@/components/FilesUploadPlus/FilesUpload.vue"; import FilesUploadPlus from "@/components/FilesUploadPlus/FilesUpload.vue";
import transformInfo from "./transformInfo.vue"; import transformInfo from "./transformInfo.vue";
import axios from "axios"; import DownLoad from "@/utils/annexDowload";
import JSZip from "jszip";
import FileSaver from "file-saver";
const backForm = ref({ const backForm = ref({
content: "" content: ""
}); });
@ -337,42 +335,11 @@ const validatorPhone = (e: any, index: any) => {
recordData.value[index].headPersonPhone = ""; recordData.value[index].headPersonPhone = "";
} }
}; };
const getFile = (url: any) => {
return new Promise((resolve: any, reject: any) => {
axios({
method: "get",
url,
responseType: "blob"
})
.then((res: any) => {
resolve(res.data);
})
.catch(error => {
reject(error.toString());
});
});
};
// //
const onDownLoad = (row: any) => { const onDownLoad = (row: any) => {
console.log(row); console.log(row);
if (row.fileList && row.fileList.length > 0) { if (row.fileList && row.fileList.length > 0) {
const arr = row.fileList; DownLoad(row.fileList);
const zip = new JSZip();
const cache = {};
const promises = [];
arr.forEach((item, i) => {
const promise = getFile(item.fileUrl).then(data => {
const file_name = i + 1 + item.fileName;
zip.file(file_name, data, { binary: true });
cache[file_name] = data;
});
promises.push(promise);
});
Promise.all(promises).then(() => {
zip.generateAsync({ type: "blob" }).then(content => {
FileSaver.saveAs(content, "附件.zip");
});
});
} else { } else {
ElMessage.error("暂无可下载文件"); ElMessage.error("暂无可下载文件");
} }

View File

@ -282,9 +282,7 @@ import { getDicList } from "@/api/modules/jxjview";
import { singleEngineer } from "@/api/modules/common"; import { singleEngineer } from "@/api/modules/common";
import FilesUploadPlus from "@/components/FilesUploadPlus/FilesUpload.vue"; import FilesUploadPlus from "@/components/FilesUploadPlus/FilesUpload.vue";
import transformInfo from "./transformInfo.vue"; import transformInfo from "./transformInfo.vue";
import axios from "axios"; import DownLoad from "@/utils/annexDowload";
import JSZip from "jszip";
import FileSaver from "file-saver";
const current = ref({ const current = ref({
files: [] files: []
}); });
@ -334,42 +332,11 @@ const validatorPhone = (e: any, index: any) => {
recordData.value[index].headPersonPhone = ""; recordData.value[index].headPersonPhone = "";
} }
}; };
const getFile = (url: any) => {
return new Promise((resolve: any, reject: any) => {
axios({
method: "get",
url,
responseType: "blob"
})
.then((res: any) => {
resolve(res.data);
})
.catch(error => {
reject(error.toString());
});
});
};
// //
const onDownLoad = (row: any) => { const onDownLoad = (row: any) => {
console.log(row); console.log(row);
if (row.fileList && row.fileList.length > 0) { if (row.fileList && row.fileList.length > 0) {
const arr = row.fileList; DownLoad(row.fileList);
const zip = new JSZip();
const cache = {};
const promises = [];
arr.forEach((item, i) => {
const promise = getFile(item.fileUrl).then(data => {
const file_name = i + 1 + item.fileName;
zip.file(file_name, data, { binary: true });
cache[file_name] = data;
});
promises.push(promise);
});
Promise.all(promises).then(() => {
zip.generateAsync({ type: "blob" }).then(content => {
FileSaver.saveAs(content, "附件.zip");
});
});
} else { } else {
ElMessage.error("暂无可下载文件"); ElMessage.error("暂无可下载文件");
} }