繁体   English   中英

WebSocket多部分/表单数据仿真可以吗?

[英]WebSocket multipart/form-data emulation possible?

有可能发送带有二进制数据的json文本吗?

我称它为multipart / form-data原因是在html表单中,我们可以使用二进制文件发送文本,如何使用websockets进行操作,这可能吗?

所以这是一个例子:

var arrayBuffer = new ArrayBuffer();

// lets say here we fill our array buffer with binary data in the browser

// so here we got 2 ways

// 1) First we can send the json text indicating that the next request will be a binary data file
// so using this way the next request will have ready an Id for the uploaded file if we are working
// with database

var obj = {};
obj.somedata = "hello This is a name for my item";
websocket.send(JSON.stringify(obj)); //first sending the text
websocket.send(arrayBuffer); //sending the binary data file


// 2) Second, This is the way I prefer most, but I don't know if this is possible
// we send a json object 

var obj = {};
obj.somedata = "hello This is a name for my item";
obj.file = arrayBuffer;
websocket.send(JSON.stringify(obj));

但问题是我无法将二进制数据放入文本json中,因此二进制数据被破坏了,在服务器端我正在使用java ...导致使用2个请求发送文件和输入文本我认为数据不是一个好方法,有什么提示吗?

如果服务器无法处理二进制数据(这可能是配置或程序出现问题,我可以通过这种方式毫无问题地发送二进制数据),则可以将数据编码为base64或将其转义为json。 但是最好的方法是修复服务器端...

如果您想使用base64方式,请查看: https : //github.com/niklasvh/base64-arraybuffer ,例如: Java中的Base64编码

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM