后端接口
复制代码
1
2
3
4
5
6
7
8
9
10
11@PostMapping("/sendFile") public R<?> sendFile(MultipartFile file) throws IOException { byte[] bytes = file.getBytes(); String originalFilename = file.getOriginalFilename(); HashMap<String, Object> headers = new HashMap<>(); headers.put("type",file.getContentType()); headers.put("filename",originalFilename); simpMessagingTemplate.convertAndSendToUser("123456","/message/chat", bytes,headers); return R.ok(); }
前端代码
复制代码
1
2
3
4
5
6
7
8
9
10
11
12this.websocket = new WebsocketApi() this.websocket.connect("/user/123456/message/chat", (res) => { if (res.headers['content-type'] == 'application/octet-stream') { console.log(res); this.download(res.binaryBody,res.headers.filename,res.headers.type) }else{ console.log(res.body); console.log(111); } })
download
复制代码
1
2
3
4
5
6
7
8
9
10
11download(byte,filename,type){ let url =URL.createObjectURL(new Blob([byte],{type:type})) console.log(url); let a=document.createElement("a") a.href=url a.download=filename a.click() URL.revokeObjectURL(url) },
相关文章: springboot 使用websocket stomp 发送消息
参考文档 : stompjs v5
最后
以上就是糊涂眼神最近收集整理的关于使用websocket stomp 发送文件后端接口前端代码的全部内容,更多相关使用websocket内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复