首先建议单独写一个针对文件下载的请求
export const downLoadModel = () => { return request({ url: 接口地址, responseType: 'blob',// 注意一定要指定responseType的内容格式等 method: 'get' }) }
绑定点击事件,进行文件流转换,一般我们会通过一个a标签去完成。
downLoadModel() { downLoadModel().then(res => { const content = res.data; const blob = new Blob([content], { type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8" }); const fileName = "xxx.xlsx"; const downloadElement = document.createElement("a"); const href = window.URL.createObjectURL(blob); downloadElement.href = href; downloadElement.download = fileName; document.body.appendChild(downloadElement); downloadElement.click(); document.body.removeChild(downloadElement); window.URL.revokeObjectURL(href); }); },