前端发送的数据,post请求将参数放请求体,后端直接生成文件流,前端需要利用blob接收,
Get请求可以通过,a标签或者通过url跳转拿到文件流;如:window.location.href="";
post请求可借助blob:
function exportReport(taskId, linePointId){
var xhr = new XMLHttpRequest();
xhr.open("post", `你的请求字段`, true);
xhr.responseType = "blob";
xhr.onreadystatechange = () => {
let blob = new Blob([xhr.response]);
if(contentType.includes("application/json")) {
blob.text().then(text => {
let json = JSON.parse(text);
layer.msg(json.msg);
});
let url = window.URL.createObjectURL(blob);
let a = document.createElement("a");
a.href = url;
a.download = fileName;
a.click();
window.URL.revokeObjectURL(url);
}
}
}
xhr.setRequestHeader("client-type","application/json"); //报错格式不正确,设置请求头
xhr.send("json.stringyfy("发送的数据对象")");
}