前端文件下载

后端提供文件流的形式

import loading from './loading'
import axios from 'axios'

export function downloadFile(url, params, tips) {
  tips = tips || "正在导出数据中..."
  loading.showLoading(tips);
  axios({
      method: 'post',
      url: url,
      params : params,
      data : params,
      processData: false,
      responseType: 'blob'
  }).then(response => {
      loading.close()
      let url = window.URL.createObjectURL(new Blob([response.data]))
      let link = document.createElement('a')
      link.style.display = 'none'
      link.href = url

      let realFileName = response.headers['content-disposition'].split('filename=')[1];
      realFileName = decodeURIComponent(realFileName);

      link.setAttribute('download', realFileName);

      document.body.appendChild(link)
      link.click()
      document.body.removeChild(link);
  }).catch((error) => {
      console.log(error);
  })
}

 

作者:Lu
链接:https://llxx.cc/web-download/
来源:Lu's Blog

THE END

打赏
< <上一篇
下一篇>>
文章目录
关闭
目 录