feat: 附件下载封装
This commit is contained in:
parent
d7a47c544d
commit
46064646c9
43
src/utils/annexDowload.ts
Normal file
43
src/utils/annexDowload.ts
Normal 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;
|
||||||
@ -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("暂无可下载文件");
|
||||||
}
|
}
|
||||||
|
|||||||
@ -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("暂无可下载文件");
|
||||||
}
|
}
|
||||||
|
|||||||
@ -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("暂无可下载文件");
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user