vue3 - - - - - post请求实现文件下载
代码如下:
proxy
.$https({
url: `/api/export`,
method: "post",
data:{
},
responseType: "blob", // 设置响应值类型
})
.then(function (response) {
// 导出的是excel
const blob = new Blob([response], {
type: "application/vnd.ms-excel",
});
const downloadUrl = window.URL.createObjectURL(blob);
const a = document.createElement("a");
a.href = downloadUrl;
a.download = "文件名"; // 这里可以根据响应头或其他逻辑设置文件名
document.body.appendChild(a);
a.click();
window.URL.revokeObjectURL(downloadUrl);
document.body.removeChild(a);
})
.catch(function (error) {
console.log(error);
})