window.location.href = baseURL + "/api/storage/download?fileName=" + encodeURI(fileName) + "&fileUrl=" + fileUrl;
window.location.href不能带请求头,添加参数
download(row.id).then(response => {
const { data, headers } = response;
const fileName = headers[‘content-disposition‘].replace(/\w+;filename=(.*)/, ‘$1‘);
let blob = new Blob([data], {type: headers[‘content-type‘]});
// let objectURL = window.URL.createObjectURL(blob);
// window.location.href = objectURL;
console.log(fileName);
let dom = document.createElement(‘a‘)
let url = window.URL.createObjectURL(blob)
dom.href = url
// dom.download = decodeURI(fileName)
dom.download = decodeURI(row.name+".xls");
dom.style.display = ‘none‘
document.body.appendChild(dom)
dom.click()
dom.parentNode.removeChild(dom)
window.URL.revokeObjectURL(url)
});
用axios或者你用的请求库,配置完header后,get请求到文件,然后用file-saver这个包把拿到的文件保存下来就好了
原文:https://www.cnblogs.com/wlh1995/p/14052161.html