JS 檔案流方式下載Excel 問題
阿新 • • 發佈:2021-10-25
JS 檔案流方式下載Excel 問題
ajax請求中需要新增responseType
型別為blob
例如:xhr.responseType = 'blob'
1、未指定responseType
2、指定responseType
Vue中如下
export const getExcel = (params) => request({
url: '/api/file',
responseType: 'blob',
method: 'get',
params
})
下載
/**
* Download file
* @param {Bolb} b
*/
export const download = (content, filename = 'name') => {
const a = document.createElement('a')
// { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8' }
// xls型別: application/vnd.ms-excel
// xlsx型別:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8
const href = URL.createObjectURL(new Blob([content], { type: 'application/vnd.ms-excel' }))
a.download = `${filename}.xls`
a.href = href
a.click()
URL.revokeObjectURL(href)
}