通过blob请求后端导出文件

后端controller

c 复制代码
 @PostMapping("/exportPlanProject2")
    public void exportActive(@RequestBody USER user, HttpServletResponse httpServletResponse) throws IOException {}

后端service

c 复制代码
    public void exportExcel2(HttpServletResponse response) throws IOException {
        OutputStream out = response.getOutputStream();
        try {
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            response.setCharacterEncoding(Charsets.UTF_8.name());
            String fileName = "首页导出";
            fileName = URLEncoder.encode(fileName, "UTF-8").replaceAll("\\+", "%20");
            response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
            wb.write(out);
        } catch (Exception e) {
            log.error("导出Excel异常{}", e.getMessage());
            throw new CustomException("导出Excel失败,请联系网站管理员!");
        } finally {
            if (wb != null) {
                try {
                    wb.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
        }
    }

前端

c 复制代码
     request({
        method: 'post',
        url: '/investment/plan/remake/exportPlanProject2',
        data: this.queryParams,
        responseType: 'blob'
      }).then(res => {
        console.log('res---',res)
        const aLink = document.createElement('a')
        var fileName = '文件名称' + moment().format('YYYYMMDDHHmmss') + '.xlsx'
        fileName = fileName.replace(/\"/g, '')
        const url = window.URL.createObjectURL(res)
        console.log('url---',url)
        aLink.href = url
        aLink.download = fileName
        aLink.click()
        aLink.remove()
        URL.revokeObjectURL(url)
      })
相关推荐
孤蓬&听雨2 个月前
Java SpringBoot使用EasyExcel导入导出Excel文件
java·spring boot·excel·导出·导入
humors2212 个月前
java实现excel导入参考资料合集
java·前端·后端·wps·导出·导入·office
Mr。轩。3 个月前
cn.afterturn.easypoi.exception.excel.ExcelExportException: Excel导出错误 -> 修正过程。
java·excel·导出
大福是小强6 个月前
016_Save_the_picture_in_Matlab中保存图片
开发语言·matlab·导出·绘图·图片·出图
子不语6 个月前
c#实现数据导出为PDF的方式
pdf·c#·导出·文档·net
winfredzhang6 个月前
如何使用 Python创建一个视频文件管理器并实现视频截图功能
python·视频·导出·截图·搜索时长播放
winfredzhang8 个月前
使用python编程的视频文件列表应用程序
python·视频·导出·播放·搜素
烂笔头18 个月前
用JSZip,FileSaver 有现成cdn的http图片或者文件地址,弄成压缩包导出,解决如果文件名字都是一样的只导出一个图片或文件的方法
前端·javascript·jszip·filesaver·导出·导出压缩包
dazhong20128 个月前
Springboot开发之 Excel 处理工具(一) -- Apache POI
spring boot·后端·excel·导出·epoil