美文网首页
js 下载Excel

js 下载Excel

作者: 海豚先生的博客 | 来源:发表于2020-03-23 15:04 被阅读0次

MIME(媒体类型)

  • application 表明是某种二进制数据
  • 二进制文件没有特定或已知的 subtype,即使用 application/octet-stream。
  • 响应头
    • Content-Type: application/octet-stream;charset=UTF-8
    • Content-Disposition: attachment;filename=文件名.xlsx

取文件名

        try {
          let params = response.headers['content-disposition'].split('=')[1]
          let res = {
            data: data,
            params: params
          }
          return Promise.resolve(res)
        } catch (err) {}

get

      apiUrl = axios.get(url,params)
      this.$request
        .apiUrl({ params: params, responseType: 'blob'或者'arraybuffer' })
        .then(res => {
          let { data, params } = res
          console.log('title:', params)
          const blobUrl = window.URL.createObjectURL(data)
          const a = document.createElement('a')
          a.style.display = 'none'
          a.download = params
          a.href = blobUrl
          a.click()
          document.body.removeChild(a)
        })

post

      apiUrl = axios.post(url,params, {
      responseType: 'arraybuffer' // 或者'blob'
      })
      this.$request
        .apiUrl(params)
        .then(res => {
          let { data, params } = res
          console.log('title:', params, res)
          const blobUrl = window.URL.createObjectURL(data)
          const a = document.createElement('a')
          a.style.display = 'none'
          a.download = params
          a.href = blobUrl
          // a.srcObject = data
          document.body.appendChild(a)
          a.click()
          document.body.removeChild(a)
          window.URL.revokeObjectURL(blobUrl)
        })

相关文章

网友评论

      本文标题:js 下载Excel

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