vue中a标签实现带header的下载excel文件

x33g5p2x  于2022-04-10 转载在 其他  
字(0.5k)|赞(0)|评价(0)|浏览(580)
  1. import Cookie from 'js-cookie'

界面:

  1. <a-button
  2. slot="extra"
  3. type="primary"
  4. @click="exportFile"
  5. ><a-icon type="download" />导出</a-button>

js方法

  1. exportFile() {
  2. fetch('http://127.0.0.1:8765/course/exportCourse/33', {
  3. method: 'GET',
  4. headers: new Headers({
  5. 'Authorization': Cookie.get('Authorization')
  6. }),
  7. })
  8. .then(res => res.blob())
  9. .then(data => {
  10. const blobUrl = window.URL.createObjectURL(data);
  11. const a = document.createElement('a');
  12. a.download = this.fileName+'.xlsx';
  13. a.href = blobUrl;
  14. a.click();
  15. });
  16. },

相关文章