在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問答/Java  HTML/ axios 獲取后端返回的二進制流轉(zhuǎn)excel失敗

axios 獲取后端返回的二進制流轉(zhuǎn)excel失敗

  • 后端用poi生成excel對象,通過流輸出,測試代碼:

ServletOutputStream out = response.getOutputStream();

        wb.write(out);
        out.flush();
        out.close();
  • 頁面用axios發(fā)送請求,根據(jù)博客上的說法,也設(shè)置了responseType: 'blob'

請求的測試部分:

export function getExcel() {
  return request({
    url: `${BASE_URL}/meta_db/excel`,
    method: 'POST',
    responseType: 'blob'
  })
}
  • 從后臺獲取的response:

clipboard.png

頁面打印的response, response.data 現(xiàn)在是一個字符串

clipboard.png

  • JS 拿到response.data, 生成Blob,生成的xls文件打開全是亂碼

測試代碼:

 getExcel().then(response => {
                console.log(response);
                console.log(typeof response.data)
                let url = window.URL.createObjectURL(new Blob([response.data], {
                    type: "application/vnd.ms-excel;charset=utf-8"
                }))
                let link = document.createElement('a')
                link.style.display = 'none'
                link.href = url
                link.setAttribute('download', 'excel.csv')
                document.body.appendChild(link)
                link.click();
            })

結(jié)果:

clipboard.png

有人知道這個問題的解決辦法嗎? 謝謝

回答
編輯回答
小曖昧

都是buffer能出什么問題,如果你能確定你excel文檔生成沒問題,就是本地系統(tǒng)打開excel出問題。想驗證很簡單,對比下buffer就行了,比如做個md5

2018年6月21日 08:03