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

// 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. },

  • 5
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用 Vue.js 中的 axios 库来下载文件保存到本地。以下是一个示例代码: ```javascript // 首先,需要安装 axios 库 // 可以通过以下命令进行安装: // npm install axios // 在你的 Vue 组件中引入 axios import axios from 'axios'; methods: { downloadFile() { // 发起 GET 请求获取文件数据 axios.get('http://example.com/file-url', { responseType: 'blob' // 声明响应类型为 blob }) .then(response => { // 创建一个 <a> 标签 const url = window.URL.createObjectURL(new Blob([response.data])); const link = document.createElement('a'); link.href = url; // 设置下载文件名 link.setAttribute('download', 'filename.ext'); // 将 <a> 标签添加到 DOM 中,模拟点击下载 document.body.appendChild(link); link.click(); // 清理临时创建的 URL 对象 window.URL.revokeObjectURL(url); }) .catch(error => { console.error(error); }); } } ``` 在上述示例代码中,`downloadFile` 方法会发起一个 GET 请求来获取文件数据。通过设置 `responseType` 为 `'blob'`,响应数据会以 Blob 对象形式返回。接着,我们创建一个临时的 URL 对象,将 Blob 对象数据赋值给 `<a>` 标签的 `href` 属性。然后,我们设置 `<a>` 标签的 `download` 属性来指定下载文件名。最后,将 `<a>` 标签添加到 DOM 中,并触发模拟点击下载操作。完成下载后,清理临时创建的 URL 对象。 请注意,你需要将 `http://example.com/file-url` 替换为实际的文件 URL,将 `'filename.ext'` 替换为你想要保存文件名和扩展名。 希望能对你有所帮助!如果有任何疑问,请随时向我提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值