dialogClose(dialogItem) {
this.$refs[dialogItem.code + "Ref"][0].resetFields();
},
//文件下载
// 入参 file:被下载文件对象
fileDownload(file) {
const loading = this.$loading({
lock: true,
text: "导出中",
spinner: "el-icon-loading",
background: "rgba(0, 0, 0, 0.7)",
});
this.$axios({
url:
this.$zjyhPath +
"/yhEvent/downloadFile?fdObjectid=" +
file.fdObjectid,
method: "get",
timeout: 180000,
responseType: "blob",
})
.then((res) => {
// 定义文件名等相关信息
loading.close();
const blob = res.data;
if ("download" in document.createElement("a")) {
const reader = new FileReader();
reader.readAsDataURL(blob);
reader.onload = (e) => {
const a = document.createElement("a");
var date = moment().format("YYYY-MM-DD hh:mm:ss");
a.download = `${file.name}${date}${file.fileext}`;
document.body.appendChild(a);
a.href = URL.createObjectURL(blob);
a.click();
URL.revokeObjectURL(blob);
document.body.removeChild(a);
};
} else {
navigator.msSaveBlob(blob, `${file.name}${date}${file.fileext}`);
}
})
.catch(() => {
loading.close();
});
},
vue实现word或pdf文档导出的功能,我的项目是:后端返回一个文档流(下图),然后前端对文档流做处理进行下载,代码如下:
import axios from 'axios';
axios.get(`url`, { //url: 接口地址
responseType: `arraybuffer` //一定要写
})
.then(res => {
if(res.status == 200){
let blob = new Blob([res.data], {
type: `application/msword` //word文档为msword,pdf文档为pdf
});
let objectUrl = URL.createObjectURL(blob);
let link = document.createElement("a");
let fname = `我的文档`; //下载文件的名字
link.href = objectUrl;
link.setAttribute("download", fname);
document.body.appendChild(link);
link.click();
}else {
this.$message({
type: "error",
message: "导出失败"
})
}
})