前端下載後端Blob流文件

後端返回的數據在network的preview中,是一堆碼。

download () {
      axios({
        method: 'get',
        url: baseUrl, //baseUrl是後端給你的下載的接口地址
        //看後臺規定的類型,responseType屬性根據你的實際情況確定
        responseType: 'arraybuffer'
      }).then(res => {
        //假設返回的數據在res中!!!
        const data = res
        const url = window.URL.createObjectURL(new Blob([data]), 
        // 要轉化的類型,我這個是xslx文件
        { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' })
        const link = document.createElement('a')
        link.style.display = 'none'
        link.href = url
        link.setAttribute('download', '院系專業信息Excel格式示例.xlsx')
        document.body.appendChild(link)
        link.click()
        document.body.removeChild(link)
      })
    },

深入理解可見:深入理解xhr的responseType中blob和arrayBuffer

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章