当前位置:   article > 正文

前端GET/POST请求下载文件多种方式_使用post请求下载文件

使用post请求下载文件

下载post

  1. export const download = (data, projectId) => {
  2. return request({
  3. url: BaseUrl + '/xxx/xxx/xxx',
  4. headers: {
  5. "Project-Id": projectId,
  6. httpWhite: true,
  7. },
  8. responseType: "blob",//文件流
  9. method: 'post',
  10. data
  11. })
  12. }
  1. <el-button size="small" type="primary" class="downLoadTemplate" @click="downloadFile(row)">
  2. <i class="iconfont yun-xiazai"></i>
  3. <span class="first">下载数据模板</span>
  4. </el-button>
  5. //点击下载
  6. const downloadFile(row){
  7. const params = {
  8. 需要传递的参数:'xxxx',
  9. id:row.id, //示例,
  10. }
  11. download(params, this.projectIds).then((res) => {
  12. if (res.status === 200) {
  13. this.downloadDataTemplate(res);
  14. }
  15. });
  16. }
  17. //下载数据模板
  18. downloadDataTemplate(res) {
  19. if (!res.data) {
  20. return;
  21. }
  22. const fileName = decodeURIComponent(
  23. res.headers["content-disposition"].split("filename=")[1]
  24. );
  25. const blob = new Blob([res.data], {
  26. type: "application/vnd.openxmlformats- officedocument.spreadsheetml.sheet;charset=utf-8",
  27. });
  28. const downloadElement = document.createElement("a");
  29. const href = window.URL.createObjectURL(blob); // 创建下载的链接
  30. downloadElement.href = href;
  31. // 文件名中有中文 则对文件名进行转码
  32. downloadElement.download = fileName; // 下载后文件名
  33. document.body.appendChild(downloadElement);
  34. downloadElement.click(); // 点击下载
  35. document.body.removeChild(downloadElement); // 下载完成移除元素
  36. window.URL.revokeObjectURL(href); // 释放blob对象
  37. },

Get下载方法

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

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

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

  1. export const exportPersonnel = (params) => request({
  2. url: BaseUrl + '/exportxxx/xxxx',
  3. headers: {
  4. "Project-Id": params.projectId,
  5. },
  6. method: 'get',
  7. responseType: 'blob', //文件流方法
  8. params,
  9. })
  1. // 导出
  2. exportcustomer() {
  3. let downStr = { ...this.params };
  4. exportPersonnel(downStr).then((r) => {
  5. if (r.status === 200) {
  6. //获取下载文件名
  7. const fileName = decodeURIComponent(
  8. r.headers["content-disposition"].split("filename=")[1]
  9. );
  10. // 创建 a 元素
  11. const link = document.createElement('a');
  12. const href = window.URL.createObjectURL(r.data)//创建下载链接
  13. link.href = href;// 设置下载链接的 URL
  14. link.style.display = "none";
  15. link.download = fileName; // 下载后文件名
  16. document.body.appendChild(link);
  17. link.click(); // 点击下载
  18. document.body.removeChild(link); // 下载完成移除元素
  19. window.URL.revokeObjectURL(href); // 释放blob对象
  20. }
  21. });
  22. },
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Li_阴宅/article/detail/784921
推荐阅读
相关标签
  

闽ICP备14008679号