The download file setting download does not take effect

The download attribute is set for the downloaded file , but the downloaded name is still garbled. It may be because the downloaded file crosses domains. The solution is as follows

    download(file) {
      const urls = file.attachLink || file.link
      const x = new window.XMLHttpRequest()
        x.open('GET', urls, true)
        x.responseType = 'blob'
        x.onload = () => {
          const url = window.URL.createObjectURL(x.response)
          const a = document.createElement('a')
          a.href = url
          a.download = file.name
          a.click()
        }
      x.send()
    },

or

      this.axios.get('接口地址', { params: params, responseType: 'blob' }).then(res => {
        const blob = new Blob([res], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8' })
        const url = window.URL.createObjectURL(blob)
        const a = document.createElement('a')
        document.body.appendChild(a)
        a.href = url
        a.download = '文件名称'
        a.click()
        window.URL.revokeObjectURL(url)
      })
    }

Guess you like

Origin blog.csdn.net/weixin_47039061/article/details/128645060