方法一 使用a标签下载
-- 使用a标签下载, download 属性,`url` 是所要下载的地址,注意IE浏览器不支持
`<a href={`${url}`} download>导出</a>`
方法二 接口返会的是二进制流, 兼容ie
let url = '后端给的下载地址'
let timestamp = new Date().getTime();
let xmlResquest = new XMLHttpRequest();
xmlResquest.open("GET", `${url}`, true);
xmlResquest.setRequestHeader("Content-type", "application/json");
xmlResquest.responseType = "blob";
xmlResquest.onload = function (oEvent) {
let content = xmlResquest.response;
if('msSaveOrOpenBlob' in navigator){
let filename = timestamp+ ".xls";
let blob = new Blob([content], {type: 'application/vnd.ms-excel'});
window.navigator.msSaveOrOpenBlob(blob, filename);
} else{
let elink = document.createElement("a");
elink.download = timestamp + ".xls";
elink.style.display = "none";
let blob = new Blob([content]);
elink.href = URL.createObjectURL(blob);
document.body.appendChild(elink);
elink.click();
document.body.removeChild(elink);
}
};
xmlResquest.upload.onprogress = function (e) {
if (e.lengthComputable) {
let pross = (e.loaded / e.total) * 100;
console.log(pross)
}
}
xmlResquest.send();