簡體   English   中英

如何使用websocket將json中的base64 url​​數據從javascript客戶端發送到nodejs服務器?

[英]How to send base64 url data using json from javascript client to nodejs server using websocket?

我正在嘗試將base64網址從javascript發送到nodejs,但是我沒有找到正確的方法來做到這一點。 如果有人知道,請幫忙! 謝謝。

這是我嘗試的代碼,

Client.js

 var image = document.getElementById("canvas").toDataURL("image/jpeg");
 var encoded = btoa(JSON.stringify(image));
 connection.send(encoded);

Server.js

connection.on('message', function(message){

           console.log("Message: "+message);
            if (message.type === 'utf8') { // accept only text
                var post = Buffer.from(message.data).toString('base64');
                 console.log("post: "+post);
            }
});

哪個錯誤:

Message: [object Object]
buffer.js:207
    throw new ERR_INVALID_ARG_TYPE(
    ^

TypeError [ERR_INVALID_ARG_TYPE]: The first argument must be one of type string, Buffer, ArrayBuffer, Array, or Array-like Object. Received type undefined

謝謝 !!

嘗試這個。

客戶

 var base64 = document.getElementById("canvas").toDataURL("image/jpeg");
 connection.send(base64);

服務器

connection.on('message', function (message) {

    console.log("Message: " + message);
    if (message.type === 'utf8') { // accept only text
        var post = message.utf8Data;
        console.log("post: " + post);
    }
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM