代码改变世界IT技术篇

React 下载文件和导出功能实现

2020-05-27  本文已影响0人  燕自浩
前提条件
1. React架构
2. TS
3. axios请求
// 下载
  goDown = (recond: any) => {
    this.axios.instance({
      method: 'post',
      url: this.api.taskDownExcel!.path,
      data: { id: recond.id },
      timeout: 10000000
    }).then((res: any) => {
      let { data, code, msg } = res.data
      if (code === 200) {
        let link = document.createElement('a')
        link.style.display = 'none'
        link.href = data
        link.setAttribute('download', `${recond.fileName}文件.xlsx`)
        document.body.appendChild(link)
        link.click()
      } else {
        this.$message.error('下载失败!')
      }
    })
  }
上一篇 下一篇

猜你喜欢

热点阅读