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