簡體   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