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' // 重点添加
    }); 
}

定义下载方法 

downloadFile(){
    const result = await downloadReportApi(params);
    if(result.status == 200){
      fileDownload(result.data,'xxxx.docx' );
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_46205984/article/details/130971519
今日推荐