(办公)vue下载excel,后台用post方法 - Go语言中文社区

(办公)vue下载excel,后台用post方法


  后台方法的参数必须是@RequestBody修饰的。

      前台关键代码:

     

axios ( {
          method : 'post',
          url : api.exportPlayTime , // 请求地址
          data : {
            choose : type,
            begindate : startDate,
            enddate : endDate
          },
          responseType : 'arraybuffer',
          observe: 'response',
        } )
          .then ( ( res ) => {

            const fileName = ""+filename+".xlsx"
            let blob = new Blob([res.data], {type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"});
            if ( 'download' in document.createElement ( 'a' ) ) { // 非IE下载
              const elink = document.createElement ( 'a' )
              elink.download = fileName
              elink.style.display = 'none'
              elink.href = URL.createObjectURL ( blob )
              document.body.appendChild ( elink )
              elink.click ()
              URL.revokeObjectURL ( elink.href ) // 释放URL 对象
              document.body.removeChild ( elink )
            } else { // IE10+下载
              navigator.msSaveBlob ( blob, fileName )
            }

          })

 

download(data) {
        if (!data) {
          return
        }
        let url = window.URL.createObjectURL(new Blob([data]))
        let link = document.createElement('a')
        link.style.display = 'none'
        link.href = url
        link.setAttribute('download', 'excel.xlsx')

        document.body.appendChild(link)
        link.click()
      },

 

版权声明:本文来源博客园,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://www.cnblogs.com/historylyt/p/10845090.html
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。
  • 发表于 2019-11-17 17:27:23
  • 阅读 ( 1210 )
  • 分类:前端

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢