js 下圖圖片、Excel、pdf

commonDownloads(res, str, type) {
    var blob = new Blob([res.data], {
      type: type
    })
    if (window.navigator && window.navigator.msSaveOrOpenBlob) { //IE兼容
      window.navigator.msSaveOrOpenBlob(blob, str)
    } else {
      var downloadElement = document.createElement('a')
      var href = window.URL.createObjectURL(blob)
      downloadElement.href = href
      downloadElement.download = str
      document.body.appendChild(downloadElement)
      downloadElement.click()
      document.body.removeChild(downloadElement)
      window.URL.revokeObjectURL(href)
    }
}

func() {
    axios({
        url,
        method,
        responseType: 'blob' : 'arraybuffer',
        success: (res) => {
            const str = `${new Date().getTime()}.xlsx`
            const type = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8'
            this.commonDownloads(res, str, type)
        }
    })
}
複製代碼
相關文章
相關標籤/搜索