vue2 点击按钮下载文件保存到本地(后台返回的zip压缩流)

发布时间:2024年01月20日

// import './mock/index.js';  // 该项目所有请求使用mockjs模拟 去掉mock
   
   
?页面url下载

   
   
  1. console.log( 'res', res)
  2. / /token 是使页面不用去登录了
  3. if (res. file) {
  4. window. location.href =
  5. Vue. prototype.$config.VUE_APP_BASE_IDSWAPI +
  6. Vue. prototype.$config.VUE_APP_IDSW +
  7. '/service/models/download?action=zip&filepath=' +
  8. encodeURIComponent(encodeURIComponent(res[ 'file'])) +
  9. ` &token =${localStorage.getItem( 'inmsToken')} &accessToken =${localStorage.getItem( 'accessToken')}`
  10. }

responseType: "blob",???

let blob = new Blob([response.data], { type: "application/zip" }); //注意是response 或者?response.data
let url = window.URL.createObjectURL(blob);? 这三句最重要!!!


   
   
  1. / /普通代码
  2. axios.post(postUrl, params, {responseType: 'arraybuffer'}). then(res = > {
  3. / / 创建Blob对象,设置文件类型
  4. let blob = new Blob([res. data], { type: "application/vnd.ms-excel"})
  5. let objectUrl = URL.createObjectURL(blob) / / 创建URL
  6. location.href = objectUrl;
  7. URL.revokeObjectURL(objectUrl); / / 释放内存
  8. })


   
   
  1. downloadAll() {
  2. axios({
  3. method: "get",
  4. url: "api/TemplateDownload/GetAllTemplateZIP",
  5. headers: {
  6. "content-type": "application/json; charset=utf-8",
  7. Authorization: Cookies. get( "token") || "",
  8. },
  9. responseType: "blob",
  10. })
  11. . then((response) = > {
  12. let blob = new Blob([response. data], { type: "application/zip" });
  13. let url = window.URL.createObjectURL(blob);
  14. const link = document.createElement( "a"); / / 创建a标签
  15. link.href = url;
  16. link.download = "模板下载"; / / 重命名文件
  17. link.click();
  18. URL.revokeObjectURL(url); / / 释放内存
  19. this.checkList = [];
  20. })
  21. .catch(( error) = > {
  22. console.log( error. data);
  23. });
  24. },
  25. / /excel
  26. let blob = new Blob([response. data], { type: "application/vnd.ms-excel" });
  27. dl() {
  28. axios({
  29. method: "get",
  30. url: "http://10.180.170.3:8794/tRptMwStdClt/exportData?time=202104",
  31. responseType: "arraybuffer",
  32. })
  33. . then((response) = > {
  34. console.log(response);
  35. let blob = new Blob([response. data], {
  36. type: "application/vnd.ms-excel",
  37. });
  38. let url = window.URL.createObjectURL(blob);
  39. const link = document.createElement( "a"); / / 创建a标签
  40. link.href = url;
  41. link.download = "模板下载"; / / 重命名文件
  42. link.click();
  43. URL.revokeObjectURL(url); / / 释放内存
  44. this.checkList = [];
  45. })
  46. .catch(( error) = > {
  47. console.log( error. data);
  48. });
  49. },

获取到了后台传过来的excel文件 前端用vue怎么接收并导出? - 中文 - Vue Forum

vue.js前端实现excel表格导出和获取headers里的信息 - 五个半柠檬 - OSCHINA - 中文开源技术交流社区

java后台需要设置


   
   
  1. response.addHeader( "Content-Disposition", "attachment;filename=" + fileName + ".xlsx");
  2. response.setHeader( "Access-Control-Expose-Headers", "Content-Disposition");
  3. 才能获取到文件名等信息


   
   
  1. / / 导出execel 2
  2. handleExcel() {
  3. this.$http({
  4. url: this.$http.adornUrl(url),
  5. method: "post",
  6. responseType: "blob", / /!!!!
  7. params: this.$http.adornParams({
  8. userAccount: this.userName,
  9. }),
  10. }). then((res) = > {
  11. / / console.log(res, "res");
  12. let blob = new Blob([res. data], { type: "application/vnd.ms-excel" });
  13. let fileName = decodeURI(
  14. response.headers[ "content-disposition"].split( ";")[ 1].split( "=")[ 1]
  15. );
  16. let url = window.URL.createObjectURL(blob);
  17. const link = document.createElement( "a"); / / 创建a标签
  18. link.href = url;
  19. link.download = fileName; / / 重命名文件
  20. link.click();
  21. URL.revokeObjectURL(url); / / 释放内存
  22. });
  23. },

文章来源:https://blog.csdn.net/weixin_64310738/article/details/135718675
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。