import { blobValidate } from "@/utils/ruoyi"; //判断是否为blob文件
import { saveAs } from "file-saver"; //需要安装file-saver插件
import { Message, Loading } from "element-ui";
let downloadLoadingInstance; //download加载框
// 验证是否为blob格式
export async function blobValidate(data) {
try {
const text = await data.text();
JSON.parse(text);
return false;
} catch (error) {
return true;
}
}
// 下载文档接口 要判断是否为blob
export function handleDownload(query) {
return request({
url: '/setUpProject/file',
method: 'get',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
responseType: 'blob',
params: query
})
}
/** 下载文档操作 */
handleDownload(row) {
downloadLoadingInstance = Loading.service({
text: "正在下载数据,请稍候",
spinner: "el-icon-loading",
background: "rgba(0, 0, 0, 0.7)",
});
handleDownload(row)
.then(async (data) => {
const isLogin = await blobValidate(data);
if (isLogin) {
const blob = new Blob([data]);
saveAs(blob, `文档.doc`);
}
downloadLoadingInstance.close();
})
.catch((err) => {
Message.error("下载文件出现错误,请联系管理员!");
downloadLoadingInstance.close();
});
},