后端接口
@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();
}
前端代码
this.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
download(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