通过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)
      })
相关推荐
又是重名了5 天前
大数据量导出 优化方案多版 第五版
数据库·优化·导出·大数据量
winfredzhang7 天前
一个 HTML 文件的备忘录:纸间备忘(Memo on Paper)源码全解析
前端·html·导出·localstorage·备忘录·json和html
core51212 天前
如何从多架构 Docker 镜像中导出指定的 ARM64 版本
华为·docker·架构·镜像·导出·多架构
猎人在吃肉2 个月前
Notepad++ 快捷键配置文件的导入和导出
notepad++·导出·快捷键·导入·配置文件
xurime2 个月前
Excelize 开源十周年,发布 2.11.0 版本
golang·开源·github·excel·导出·导入·excelize·基础库
Highcharts.js5 个月前
Highcharts 前端导出详解:如何实现纯客户端导出(Offline Exporting)
前端·客户端·导出·highcharts·导出图片
Highcharts.js5 个月前
Highcharts 命令行渲染指南:如何使用 Node 导出服务器批量生成图表图片
服务器·数据可视化·导出·命令行·图表·highcharts
Highcharts.js7 个月前
【Highcharts】如何用命令行渲染导出图片?
javascript·导出·开发文档·highcharts·命令行渲染·命令行功能
jogging8 个月前
mysql导出表结构信息到excel【DBeaver】
mysql·oracle·excel·导出·表结构
Access开发易登软件10 个月前
Access导出带图表的 HTML 报表:技术实现详解
数据库·后端·html·vba·导出·access