1、后台正常返回流即可
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26@RequestMapping(value = "/download/fileword", method = RequestMethod.GET) public void downLoad(HttpServletResponse response) { try (InputStream in = new FileInputStream(“G://test.docx”);){ //String gFileName = URLEncoder.encode(fileName, "UTF-8"); //如进行下载名为:文件(3).txt,下载时显示名为:文件+(3).txt --空格变为了+号 //解决办法如下 //String dFileName = gFileName.replaceAll("\+", "%20"); //设置输出的格式 response.reset(); //去除前后空格 //激活浏览器弹出窗口 //response.setContentType("application/x-msdownload"); //浏览器弹出窗口显示的文件名 //response.addHeader("Content-Disposition", "attachment;filename="+dFileName); byte[] b = new byte[1024]; int len; while ((len = in.read(b)) > 0){ response.getOutputStream().write(b, 0, len); } // File file = new File(WordText.fileNew); // file.delete(); } catch (Exception e){ e.printStackTrace(); } }
2、前台js代码
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20var url = "http://192.168.15.127:8080/download/fileword”; fetch(url, { method: 'GEt', headers: {'Content-Type': 'application/json'}, //body: '<请求参数:json字符串>', }).then(res => res.blob()).then(data => { alert("1112"); var blobUrl = window.URL.createObjectURL(data); downloads(blobUrl); }); function downloads(blobUrl) { const a = document.createElement('a'); a.style.display = 'none'; a.download = 'aa.docx'; a.href = blobUrl; a.click(); document.body.removeChild(a); }
最后
以上就是繁荣手套最近收集整理的关于后台返回文件流,监听文件下载结束的全部内容,更多相关后台返回文件流内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复