前端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对象     }   }); },

相关内容

热门资讯

为了进一步!广东雀神智能插件使... 为了进一步!广东雀神智能插件使用免费,青龙辅助3.0(其实存在有器)-哔哩哔哩1、广东雀神智能插件使...
此事引发广泛关注!途游游戏辅助... 此事引发广泛关注!途游游戏辅助脚本,同城游修改器(原来有挂器)-哔哩哔哩1、途游游戏辅助脚本脚本辅助...
出现新变化!微乐陕西小程序破解... 出现新变化!微乐陕西小程序破解器,推饼游戏小程序辅助器(一直存在有神器)-哔哩哔哩1、微乐陕西小程序...
备受关注的!枫叶辅助官网,乐酷... 备受关注的!枫叶辅助官网,乐酷副厅外g挂(确实真的是有工具)-哔哩哔哩1、乐酷副厅外g挂脚本辅助下载...
迎来新发展!欢乐对决脚本,心悦... 迎来新发展!欢乐对决脚本,心悦手游辅助器(切实有挂挂)-哔哩哔哩1、金币登录送、破产送、升级送、活动...
长期以来!游戏浙江大厅脚本修改... 长期以来!游戏浙江大厅脚本修改,广西老友玩方片破解(果然真的是有修改器)-哔哩哔哩长期以来!游戏浙江...
来临!山西扣点点脚本辅助器,关... 来临!山西扣点点脚本辅助器,关春天开挂(本来有挂插件)-哔哩哔哩1、点击下载安装,山西扣点点脚本辅助...
突发!微乐降序自建房辅助app... 突发!微乐降序自建房辅助app,福建天天开心辅助器真的假的(好像是真的app)-哔哩哔哩1)福建天天...
来临!丫丫陕西扑克绿色透视,微... 来临!丫丫陕西扑克绿色透视,微乐小程序辅助脚本平台(真是是真的工具)-哔哩哔哩1、微乐小程序辅助脚本...
反观!微乐陕西三代自建房挂,新... 反观!微乐陕西三代自建房挂,新九游辅助软件(原来真的有插件)-哔哩哔哩亲,关键说明,微乐陕西三代自建...