vue2.x 获取流文件乱码问题,以及下载的文件名变更

本文介绍了在Vue2.x中遇到的流文件下载时出现的乱码问题,以及如何解决浏览器兼容性问题。同时,文章详细讲解了如何变更下载的Excel文件名称,提供了后台处理文件流的代码示例。

这个方式存在浏览器兼容问题!!!

参考博文:vue2.x 下载后台传过来的流文件(excel)后乱码问题

变更下载文件名:

toExcelExport(){

        this.$axios.post(url.paths.storageWarehouse.warehouseExcel,{params:this.$data.search},{responseType: 'arraybuffer'}).then((res) => {

          let blob = new Blob([res], {type: "application/vnd.ms-excel"});

          var link = document.createElement('a');

          link.href = window.URL.createObjectURL(blob);

          link.download = "仓库列表";

          link.click();

        });
      }
但是这种方式只支持 chrome/firefox

整个实现的全部代码:

后台:

    @RequestMapping(value = "/warehouseExcel", method = RequestMethod.POST)
    @RequireToken
    public void modelExcel(StorageWarehouse storageWarehouse, Integer excelType, HttpServletRequest request, HttpServletResponse response) {
        OutputStream outputStream = null;
        try {
            HSSFWorkbook hssfWorkbook = createmodelExcel(storageWarehouse);
            String fileName = "仓库列表" + System.currentTimeMillis();
            response.setContentType("application/vnd.ms-excel;charset=utf-8");
            response.setHeader("Content-Disposition", "attachment;filename="
                    + new String((fileName + ".xls").getBytes(), "iso-8859-1"));
            outputStream = response.getOutputStream();
            hssfWorkbook.write(outputStream);
            outputStream.flush();
        } catch (Exception e) {
            logger.error(e.getMessage());
        }finally {
            if (outputStream != null) {
                try {
                    outputStream.close();
                } catch (IOException e) {

                }
            }
        }
    }
前台:

toExcelExport(){

        this.$axios.post(url.paths.storageWarehouse.warehouseExcel,{params:this.$data.search},{responseType: 'arraybuffer'}).then((res) => {

          let blob = new Blob([res], {type: "application/vnd.ms-excel"});

          var link = document.createElement('a');

          link.href = window.URL.createObjectURL(blob);

          link.download = "仓库列表";

          link.click();

        });
      },






评论 6
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值