前端如何下载文件

xls,xlsx 格式
 
if (res.status == 200) {
const content = res.data;
const blob = new Blob([content]);
const fileName = "XXXXX.xls";
if ("download" in document.createElement("a")) {
// 非IE下载
const elink = document.createElement("a");
elink.download = fileName;
elink.style.display = "none";
elink.href = URL.createObjectURL(blob);
document.body.appendChild(elink);
elink.click();
URL.revokeObjectURL(elink.href); // 释放URL 对象
document.body.removeChild(elink);
} else {
// IE10+下载
navigator.msSaveBlob(blob, fileName);
}
 
pdf 格式
this.$http({
method: "post",
responseType: "arraybuffer",
url: "http://172.16.2.77:9001/api/RepairProcess/GetHistoryForDPF",
data:qs.stringify(params)
}).then(res => {
if (res.status == 200) {
let blob = new Blob([res.data], {
type: `application/pdf` //word文档为msword,pdf文档为pdf
});
let objectUrl = URL.createObjectURL(blob);
let link = document.createElement("a");
let fname = this.RepairId; //下载文件的名字
link.href = objectUrl;
link.setAttribute("download", fname);
document.body.appendChild(link);
link.click();
} else {
this.$message({
type: "error",
message: "导出失败"
});
}
});
posted @ 2018-11-28 11:38  Angel-01  阅读(876)  评论(0编辑  收藏  举报