前端GET/POST请求下载文件多种方式
创始人
2025-01-15 06:07:55
0

下载post

export const download = (data, projectId) => {   return request({     url: BaseUrl + '/xxx/xxx/xxx',     headers: {       "Project-Id": projectId,       httpWhite: true,     },     responseType: "blob",//文件流     method: 'post',     data   }) }
          下载数据模板  //点击下载 const downloadFile(row){   const params = {     需要传递的参数:'xxxx',     id:row.id,  //示例,   }     download(params, this.projectIds).then((res) => {     if (res.status === 200) {       this.downloadDataTemplate(res);     }   }); }  //下载数据模板 downloadDataTemplate(res) {   if (!res.data) {     return;   }   const fileName = decodeURIComponent(     res.headers["content-disposition"].split("filename=")[1]   );   const blob = new Blob([res.data], {     type: "application/vnd.openxmlformats-  officedocument.spreadsheetml.sheet;charset=utf-8",   });   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); // 释放blob对象 }, 

Get下载方法

通用get下载方法 99%可以使用
const downError = `BaseUrl+/xxx/xxx/xxxx?${this.tokenHeader}=${getToken()}&projectId=${this.projectId}&spaceId=${this.spaceId}`;  window.open(downError, "_self");//调用window方法
特殊get下载方法 1%才能用到 一般用不到 (仅用于个人记录)

这种使用于需要在hearder里面添加projecrt-Id等参数 一般的都不需要 主要看后端接口能不能使用

使用下面这种方法 最主要get下载的请求 是responseType:blob 文件流

export const exportPersonnel = (params) => request({   url: BaseUrl + '/exportxxx/xxxx',   headers: {     "Project-Id": params.projectId,   },   method: 'get',   responseType: 'blob',		//文件流方法   params, })
// 导出 exportcustomer() {   let downStr = { ...this.params };   exportPersonnel(downStr).then((r) => {     if (r.status === 200) {       //获取下载文件名        const fileName = decodeURIComponent(         r.headers["content-disposition"].split("filename=")[1]       );       // 创建 a 元素       const link = document.createElement('a');       const href = window.URL.createObjectURL(r.data)//创建下载链接       link.href = href;// 设置下载链接的 URL       link.style.display = "none";        link.download = fileName; // 下载后文件名       document.body.appendChild(link);       link.click(); // 点击下载       document.body.removeChild(link); // 下载完成移除元素       window.URL.revokeObjectURL(href); // 释放blob对象     }   }); },

相关内容

热门资讯

26年最新开挂版!方片十三张辅... 您好,方片十三张这款游戏可以开挂的,确实是有挂的,需要了解加去威信【136704302】很多玩家在这...
26年最新开挂版!凯旋麻将辅助... 26年最新开挂版!凯旋麻将辅助透视挂,君澜娱乐确实有外挂,本来有挂方法一、君澜娱乐可以开透视的定义与...
26年最新开挂版!来来来块块猫... 26年最新开挂版!来来来块块猫辅助透视挂,众悦棋牌是有外挂,本来有挂分享1、很好的工具软件,可以解锁...
26年最新开挂版!微乐双扣辅助... 26年最新开挂版!微乐双扣辅助透视挂,同乡游麻将真的是有外挂,果然了解有挂微乐双扣破解侠是真的助透视...
26年最新开挂版!樱花之盛辅助... 26年最新开挂版!樱花之盛辅助透视挂,云雀麻将蕲春是真的有外挂,好像有挂规律亲,关键说明,云雀麻将蕲...
26年最新开挂版!福运辅助透视... 26年最新开挂版!福运辅助透视挂,全民松原麻将真的有外挂,一贯有挂细节1、起透看视 福运辅助软件价格...
26年最新开挂版!玉溪娱乐室辅... 26年最新开挂版!玉溪娱乐室辅助透视挂,泰州麻将是真的有外挂,本来了解有挂1、超多福利:超高返利,海...
26年最新开挂版!开心联盟辅助... 26年最新开挂版!开心联盟辅助透视挂,情怀信阳是有外挂,果然有挂方略1、进入到开心联盟是否有挂之后,...
26年最新开挂版!微乐河南麻将... 26年最新开挂版!微乐河南麻将辅助透视挂,起来麻将是有外挂,一直有挂技巧1、金币登录送、破产送、升级...
26年最新开挂版!快玩炸翻天辅... 26年最新开挂版!快玩炸翻天辅助透视挂,乐乐是有外挂,竟然有挂透明挂快玩炸翻天是不是有人用挂微扑克w...