JS 下载文件

一、背景

二、实现

点击查看代码
// 下载文件
export function downloadFile(obj, name, suffix) {
  const url = window.URL.createObjectURL(new Blob([obj]))
  const link = document.createElement('a')
  link.style.display = 'none'
  link.href = url
  const fileName = parseTime(new Date()) + '-' + name + '.' + suffix
  link.setAttribute('download', fileName)
  document.body.appendChild(link)
  link.click()
  document.body.removeChild(link)
}

使用

点击查看代码
exportDayStatistics(params).then(result => {
        downloadFile(result, '泥处理日报表', 'xlsx')
      }).finally(() => { this.crud.downloadLoading = false })
	  
	  
export function exportMonthStatistics(params) {
  return request.post(`api/reportMud/export/month`, params, {
    responseType: 'blob' // 这个记得写
  })
}

前提是 后端返回值result 返回是对应文件的输出流。

image

三、遇到的报错

四、参考博客

posted @ 2023-09-20 17:10  林财钦  阅读(131)  评论(0)    收藏  举报