首先在请求参数中设置responseType类型为arraybuffer对象
this.$http
.get(
"/cuttingFluidSterilizationSystem/export/" +
this.clientId +
"?" +
"startDate=" +
this.startDate +
"&endDate=" +
this.endDate,
{
responseType: "arraybuffer"
}
)
然后拿到返回之后进行如下操作,兼容ie10以上和非ie操作
const content = res;
const blob = new Blob([content], {
type: "application/vnd.ms-excel"
});
const fileName = "xxxx.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);
}