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

相关内容

热门资讯

透视好友!wepoker辅助器... 透视好友!wepoker辅助器有哪些功能(透视)本来真的是有辅助工具(有挂辅助)-哔哩哔哩1、wep...
外挂手筋!pokerworld... 外挂手筋!pokerworld修改器,pokemmo脚本-切实是有辅助脚本(哔哩哔哩)1、下载好po...
来一盘!wepoker辅助插件... 来一盘!wepoker辅助插件功能,浙江游戏大厅app制作辅助,都是有辅助工具(揭秘有挂)1、玩家可...
透视教程!pokemmo手机脚... 透视教程!pokemmo手机脚本辅助器(透视)本来是真的辅助插件(有挂教学)-哔哩哔哩pokemmo...
外挂教程书!uupoker透视... 外挂教程书!uupoker透视,we poker免费辅助器-本来存在有辅助插件(哔哩哔哩)1、每一步...
技术分享!德普之星辅助器怎么用... 技术分享!德普之星辅助器怎么用,财神十三章有哪些辅助功能,真是有辅助教程(有挂透视)1)财神十三章有...
现有关情况通报如下!wepok... 现有关情况通报如下!wepoker透视破解版(透视)一直真的有辅助下载(有挂教学)-哔哩哔哩1、每一...
外挂演示!德州局透视,德州透视... 外挂演示!德州局透视,德州透视脚本-原来真的是有辅助脚本(哔哩哔哩);1)德州透视脚本有没有挂:进一...
分享个大家!约局吧作必弊脚本,... 分享个大家!约局吧作必弊脚本,欢聚水鱼辅助插件,真是真的有辅助辅助(揭秘有挂)1、每一步都需要思考,...
透视存在!智星德州插件(透视)... 透视存在!智星德州插件(透视)真是存在有辅助安装(有挂技巧)-哔哩哔哩智星德州插件辅助器是一种具有地...