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

相关内容

热门资讯

第2分钟了解!大宝苏北麻将怎么... 第2分钟了解!大宝苏北麻将怎么开挂(辅助挂)一贯是有挂(详细辅助插件);大宝苏北麻将怎么开挂软件透明...
第5分钟脚本!口袋上饶棋牌辅助... 自定义口袋上饶棋牌辅助系统规律,只需要输入自己想要的开挂功能,一键便可以生成出微扑克专用辅助器,不管...
盘点几款!广西友乐免费辅助(辅... 此外,数据分析德州(广西友乐免费辅助)辅助神器app还具备辅助透视行为开挂功能,通过对客户广西友乐免...
第5分钟了解!白金岛辅助怎么设... 第5分钟了解!白金岛辅助怎么设置(辅助挂)切实有挂(详细辅助神器)1、玩家可以在白金岛辅助怎么设置软...
7分钟有挂!新祥心挂机(辅助挂... 7分钟有挂!新祥心挂机(辅助挂)德州教程(本来真的是有挂);无聊就玩这款新祥心挂机真的有辅助,无限炸...
科技分享!九江讨赏辅助(辅助挂... 科技分享!九江讨赏辅助(辅助挂)原来真的有挂(详细辅助安装教程),支持语音通讯、好友开房及战队组建,...
第一分钟辅助器!家乡大二辅助工... 第一分钟辅助器!家乡大二辅助工具(辅助挂)2025新版(原生真的有挂)1)家乡大二辅助工具辅助挂:进...
第八分钟了解!微新欢乐卡五星辅... 1、第八分钟了解!微新欢乐卡五星辅助(辅助挂)都是是有挂(详细辅助工具)2、进入游戏-大厅左侧-新手...
推荐一款!牛总管辅助免费版(辅... 推荐一款!牛总管辅助免费版(辅助挂)果然真的有挂(详细辅助详细教程);支持2-10人实时对战,虚拟庄...
七分钟有挂!大菠萝提高好牌几率... 七分钟有挂!大菠萝提高好牌几率(辅助挂)wepoke教程(一向有挂)1)大菠萝提高好牌几率辅助挂:进...