优秀的编程知识分享平台

网站首页 > 技术文章 正文

vue下载excel文件方法

nanyue 2025-05-15 20:05:54 技术文章 2 ℃

1、访问接口加上: responseType: ‘blob’

2、js 方法: 文件下载的方法

/**
* data: 接口返回的数据
* fileName: 文件名称

* 文件名可从头拿:let _fileName = res.headers['content-disposition'].split(';')[1].split('=')[1];
*/
downloadExcel(data, fileName){
  let blob = new Blob([data], {type: "application/vnd.ms-excel"});

  const downloadElement = document.createElement('a');

  const href = window.URL.createObjectURL(blob); // 创建下载的链接

  downloadElement.href = href;

  downloadElement.download = decodeURI(fileName); // 下载后文件名

  document.body.appendChild(downloadElement);

  downloadElement.click(); // 点击下载

  document.body.removeChild(downloadElement); // 下载完成移除元素

  window.URL.revokeObjectURL(href); // 释放掉blob对象
}
最近发表
标签列表