vue项目用到的方法:下载文件

1.下载文件

 async function exportReport() {
      if (!gridData.value || gridData.value.length === 0) {
        ElMessage.warning({
          message: '未查询到数据!',
          duration: 1500
        })
        return
      }
      let requestData: any = {
        id: dataForm.currentId,
        beginDate: dataForm.matchDate && dataForm.matchDate.length > 0 ? moment(dataForm.matchDate[0]).format('YYYY-MM-DD') : undefined,
        endDate: dataForm.matchDate && dataForm.matchDate.length > 1 ? moment(dataForm.matchDate[1]).format('YYYY-MM-DD') : undefined
      }
      let data: any = await service.exportR(requestData)
      downloads(data)
    }

    function downloads(res: any) {
      let data = res.data
      let name = res.headers['filename']
      if (!name) {
        ElNotification({
          message: '文件不存在!',
          type: 'warning'
        })
        return
      }
      let showname = decodeURIComponent(name)
      let url = window.URL.createObjectURL(new Blob([data]))
      let link = document.createElement('a')
      link.style.display = 'none'
      link.href = url
      link.setAttribute('download', `${showname}`)
      document.body.appendChild(link)
      link.click()
      document.body.removeChild(link)
      window.URL.revokeObjectURL(url)
    }

你可能感兴趣的:(vue.js,javascript,前端)