the requirement is that the page has a download function, and the downloaded target file is the excel file. I use axios to request the API. If the request is successful, I can get the excel file, but the content in the excel file is incorrect. The content in the excel is the response body content returned by the interface, a long string, not the correct excel content
.export function getBlobRequest(url, params = {}) {
return new Promise((resolve, reject) => {
axios.get(url, {
params: params,
responseType: "blob" //arraybuffer,blob,document,json,text,streamjson
})
.then(response => {
resolve(response);
})
.catch(err => {
console.log(``);
Message.error({message: "!"});
reject(err);
})
})
}
blob parsing code
if (!data) {
return;
}
let head = data.headers["content-disposition"];
let type = data.headers["content-type"];
console.log(type)
let url = window.URL.createObjectURL(new Blob([data.data], {type: type}));
let fname = "";
if (head) {
try {
fname = head.split(";")[2].split("=")[1];
let reg = new RegExp(""","g");
fname = fname.replace(reg, "");
}catch (err){
console.log("can not get pdf name");
}
}
let link = document.createElement("a");
link.style.display = "none";
link.href = url;
link.setAttribute("download", fname);
document.body.appendChild(link);
link.click();
document.body.removeChild(link); //
window.URL.revokeObjectURL(url); //blob