下载失败数据和封装下载hooks

This commit is contained in:
NiSen
2023-06-20 21:59:37 +08:00
parent d94afe2958
commit a18802f1c6
2 changed files with 29 additions and 12 deletions

View File

@@ -130,6 +130,7 @@ import { Form, message } from "ant-design-vue";
import axios from "axios";
import Cookies from "vue-cookies";
import { downloadErrorData } from '@/api/case'
import useDownload from '@/hooks/useDownload'
defineProps({
visible: {
@@ -251,17 +252,13 @@ const handleSearch = () => {
//下载模版
const downloadErrorInfo = () => {
downloadErrorData({ importId: state.importId })
// apiCase.exportCasesInfo(type).then(res => {
// const link = document.createElement('a');// 创建a标签
// let blob = new Blob([res.data], { type: 'application/vnd.ms-excel' }); // 设置文件类型
// link.style.display = "none";
// link.href = URL.createObjectURL(blob); // 创建URL
// link.setAttribute("download", "案例.xls");
// document.body.appendChild(link);
// link.click();
// document.body.removeChild(link);
// })
// if (!state.importId) return
downloadErrorData({ importId: state.importId }).then((res) => {
if(res.status!=200) return
useDownload(res,'失败数据.xsl')
}).catch((error) => {
console.log(error);
})
}
// 下一步

20
src/hooks/useDownload.js Normal file
View File

@@ -0,0 +1,20 @@
/**
*
* @param {*} data 文件流Blob对象
* @param {*} mimeType 类型
* @param {*} fileName 名字
*/
const useDownload = (data, fileName, mimeType = 'application/vnd.ms-excel;charset=UTF-8') => {
const link = document.createElement('a');// 创建a标签
let blob = new Blob([data], { type: mimeType }); // 设置文件类型
link.style.display = "none";
link.href = URL.createObjectURL(blob); // 创建URL
link.setAttribute("download", fileName);
document.body.appendChild(link);
link.click();
URL.revokeObjectURL(link.href);
document.body.removeChild(link);
}
export default useDownload