目录

后台导出txt文件excel文件,前端用Blob接收

目录

后台导出txt文件、excel文件,前端用Blob接收

工作中根据实际需要将查询数据库中的数据生成txt文件,而前端使用Blob接收。

后台代码如下:

private void getData2Stream(List<Map<String,String>> list,HttpServletResponse resp)throws Exception{
        resp.setCharacterEncoding("UTF-8");
        resp.setHeader("Content-Disposition",
                "attachment;filename=" + java.net.URLEncoder.encode("test.txt", "UTF-8"));
        //xxxx    //业务代码
      resp.getOutputStream().write(sb.toString().getBytes());//将数据写入输出流中,返回给前端即可

      //关闭流
      resp.getOutputStream().flush();
      resp.getOutputStream().close();
}

前端代码可以参考: