vue前端下载文件

5/31/2023 Vue

# 方法一

window.location.href =文件请求地址

# 方法二

使用js-file-download插件下载
安装js-file-download

npm install js-file-download --save
页面中引入
import fileDownload from "js-file-download";
定义下载接口

export function downloadReportApi(params){
   return request({
       url:'xxx',
       method:'get',
       params:params,
       responseType:'blob' // 重点添加
   }); 
}
1
2
3
4
5
6
7
8

定义下载方法

downloadFile(){
   const result = await downloadReportApi(params);
   if(result.status == 200){
     fileDownload(result.data,'xxxx.docx' );
   }
}
1
2
3
4
5
6
Last Updated: 12/28/2023, 5:03:22 PM