关于后台返回的文件流下载方法

1.开发中一直使用的插件来做的var fileDownload = require('js-file-download'),但是发现safari下载不了 于是更改方法

2.

this.$axios({
                    method: 'post',
                    url: '/api/market/exportEmployee.do',
                    // headers里面设置token
                    headers: {
                      'Content-Type': 'application/json',
                      // "token":window.sessionStorage.getItem('token')
                    },
                    data: {
                        'department_id': parseInt(window.sessionStorage.getItem("departmentId")),
                          'startTime': this.date +'-1',
                          'endTime': this.date +'-31'
                    },
                    // 二进制流文件,一定要设置成blob,默认是json
                    responseType: 'blob'

                }).then(res => {
                    console.log(res)
                    if(!res.data){
                               return
                           }
                           var name = this.date + "月" + this.departmentName +"销售分析统计.xls";
                           var blob = new Blob([res.data]);
                           var url = window.URL.createObjectURL(blob);
                           var aLink = document.createElement("a");
                           aLink.style.display = "none";
                           aLink.href = url;
                           aLink.setAttribute("download", name);
                           document.body.appendChild(aLink);
                           aLink.click();
                           document.body.removeChild(aLink); //下载完成移除元素
                           window.URL.revokeObjectURL(url); //释放掉blob对象
                  })

  

原文地址:https://www.cnblogs.com/yn-cn/p/14822566.html