The front end downloads the streaming file from the background

The front end downloads the streaming file from the background

1.window.open(url,’_blank’)

2. Use Blob objects

downfile() {
    
    
      axios
        .post(postUrl, params, {
    
     responseType: 'arraybuffer' })
        .then((res) => {
    
    
          const link = document.createElement('a')
          let blob = new Blob([res.data], {
    
     type: 'application/vnd.ms-excel' })
          let href = window.URL.createObjectURL(blob) // 创建下载URL
          link.href = href
          link.download = 'ces.xlsx' // 自定义下载后文件名
          link.click() // 点击下载文件
          document.body.removeChild(downloadElement) //下载完成移除元素
          window.URL.revokeObjectURL(href) // 释放掉blob对象
        })
    },

3. Use js-file-download

download

npm install js-file-download --save

Example

import fileDownload from 'js-file-download'

downfile(){
    
    
    axios.post(postUrl, params, {
    
    responseType: 'arraybuffer'}).then(res => {
    
    
      fileDownload(res.data, 'ces.xlsx')
    });
}

Guess you like

Origin blog.csdn.net/weixin_43881166/article/details/114930329