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

相关内容

热门资讯

今年以来!新众亿免费辅助,微友... 今年以来!新众亿免费辅助,微友联盟辅助,攻略教程(果然是有挂)-哔哩哔哩;无需打开直接搜索加(薇:1...
来临!德州圈脚本,佛手在线大菠... 您好:这款佛手在线大菠萝技巧游戏是可以开挂的,确实是有挂的,很多玩家在这款佛手在线大菠萝技巧游戏中打...
来临!遇悦游戏辅助,九九山城插... 较多好评“微乐万能挂官网”开挂(透视)辅助教程 了解更多开挂安装加(136704302)微信号是一款...
此事备受玩家关注!hhpoke... 此事备受玩家关注!hhpoker辅助器视频,hhpoker是正规的吗,烘培教程(其实透视真的是有挂)...
无独有偶!欢乐达人暗宝辅助,科... 无独有偶!欢乐达人暗宝辅助,科乐填大坑辅助码,办法教程(其实是有挂)-哔哩哔哩;无需打开直接搜索加(...
记者获悉!欢乐茶馆修改器,边锋... 边锋老友填大坑辅助 无需打开直接搜索微信:136704302本司针对手游进行,选择我们的四大理由: ...
昨日!hhpoker买挂,we... wepokerplus辅助开挂教程视频分享装挂详细步骤在当今的网络游戏中,wepokerplus辅助...
现场直击!途游四川麻将辅助器软... 现场直击!途游四川麻将辅助器软件,四川途游辅助软件,要领教程(一直确实是有挂的)-哔哩哔哩;无需打开...
记者获悉!hhpoker辅助软... 记者获悉!hhpoker辅助软件是真的么,wepoker黑侠破解,指南书教程(一直透视确实有挂)-哔...
出现新变化!新518互娱脚本下... 出现新变化!新518互娱脚本下载,阿拉斗牌辅助视频,方式教程(果然是有挂)-哔哩哔哩您好:阿拉斗牌辅...