我是靠谱客的博主 糊涂眼神,这篇文章主要介绍使用websocket stomp 发送文件后端接口前端代码,现在分享给大家,希望可以做个参考。

后端接口

 @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

最后

以上就是糊涂眼神最近收集整理的关于使用websocket stomp 发送文件后端接口前端代码的全部内容,更多相关使用websocket内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(53)

评论列表共有 0 条评论

立即
投稿
返回
顶部