Front End Axios Download Excel

Source: Internet
Author: User

Requirement: Download Excel file via backend interface, no file address on back end, return binary stream file
Implementation: Axios (Ajax-like)
Main code:

axios:设置返回数据格式为blob或者arraybuffer如:    var instance = axios.creat({         ... //一些配置        responseType: ‘blob‘, //返回数据的格式,可选值为arraybuffer,blob,document,json,text,stream,默认值为json    })请求时的处理:  getExcel().then(res => {      //这里res.data是返回的blob对象           var blob = new Blob([res.data], {type: ‘application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8‘}); //application/vnd.openxmlformats-officedocument.spreadsheetml.sheet这里表示xlsx类型      var downloadElement = document.createElement(‘a‘);      var href = window.URL.createObjectURL(blob); //创建下载的链接      downloadElement.href = href;      downloadElement.download = ‘xxx.xlsx‘; //下载后文件名      document.body.appendChild(downloadElement);      downloadElement.click(); //点击下载      document.body.removeChild(downloadElement); //下载完成移除元素      window.URL.revokeObjectURL(href); //释放掉blob对象  })

Front End Axios Download Excel

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.