JS 文件流方式下載Excel 問題


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) } 
   

 
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM