美文网首页TypeScript
JS 文件流方式下载Excel 问题

JS 文件流方式下载Excel 问题

作者: 修朋飞 | 来源:发表于2020-09-09 16:35 被阅读0次

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

    相关文章

      网友评论

        本文标题:JS 文件流方式下载Excel 问题

        本文链接:https://www.haomeiwen.com/subject/cwoeektx.html