[英]JavaScript file API to node.js how to send ArrayBuffer with data through websocket
嗨,我正在嘗試從JavaScript客戶端發送文件並將其上傳到node.js服務器,並且它確實可以正常工作,客戶端:
var ws = new WebSocket("ws://localhost");
ws.binaryType = "arraybuffer";
//file input code, it all works etc...
loader.onload = (e) {
ws.send(e.target.result); //actually works and sends the arraybuffer
}
loader.readAsArrayBuffer(file/*not quoted here but you get the idea*/)
服務器端並不僅僅只是基本的websocket服務器,它確實會接收arraybuffer(以buffer的形式)。
問題:我還需要同時發送文件名和數據,如何在客戶端的ArrayBuffer中添加文件頭,並在節點中讀取它?
您可以將ArrayBuffer封裝在帶有文件名或元數據的對象中嗎? 即
var ws = new WebSocket("ws://localhost");
ws.binaryType = "arraybuffer";
// the object to be passed by the socket
var filePayload = {
fileName:'file.file',
fileType:'xxx'
// any othere metadata here
};
//file input code, it all works etc...
loader.onload = (e) => {
// add the array buffer as a property in the object
filePayload['data'] = e.target.result;
ws.send(filePayload);
}
loader.readAsArrayBuffer(file/*not quoted here but you get the idea*/)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.