Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/39.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何使用Vanilla Node.js接收blob?_Javascript_Node.js_Ajax_Blob - Fatal编程技术网

Javascript 如何使用Vanilla Node.js接收blob?

Javascript 如何使用Vanilla Node.js接收blob?,javascript,node.js,ajax,blob,Javascript,Node.js,Ajax,Blob,我有获取请求主体的普通代码,但它会创建一个字符串。到目前为止,这在大多数情况下都很有效,但现在我想收到一个blob 首先,我现在拥有的代码: http.createServer(function (request, response) { var body = ''; request.on('data', function (data) { //this works great for UTF-8, but does not work for Blobs body +=

我有获取请求主体的普通代码,但它会创建一个字符串。到目前为止,这在大多数情况下都很有效,但现在我想收到一个blob

首先,我现在拥有的代码:

http.createServer(function (request, response) {
  var body = '';

  request.on('data', function (data) {
    //this works great for UTF-8, but does not work for Blobs
    body += data;

    // Too much POST data, kill the connection!
    // 1e6 === 1 * Math.pow(10, 6) === 1 * 1000000 ~~~ 1MB
    if (body.length > 1e7) {
        console.log("POSTED data too large!")
        request.connection.destroy();
    }
  });

  request.on('end', function () {
    var pathname = "test.png";
    fs.writeFileSync(pathname, body, {flag: "w"});

    response.writeHead(200, {
      'Content-Type': 'text/plain',
      "Access-Control-Allow-Origin" : "*"
    });
    response.end("got it")
  });

}).listen(8888);
客户端:

var imgNode; //assume is loaded <img>
var canvas = document.createElement("canvas");
canvas.width = imgNode.naturalWidth;
canvas.height = imgNode.naturalHeight;
var ctx = canvas.getContext("2d");
ctx.drawImage(imgNode, 0, 0);  

canvas.toBlob(function(blob) {
  Ajax.POST("localhost:8888", blob);  //functional Ajax POST
});
var-imgNode//假设已加载
var canvas=document.createElement(“canvas”);
canvas.width=imgNode.naturalWidth;
canvas.height=imgNode.naturalHeight;
var ctx=canvas.getContext(“2d”);
ctx.drawImage(imgNode,0,0);
canvas.toBlob(函数(blob){
POST(“localhost:8888”,blob);//功能性Ajax POST
});

这里的问题是,此代码仅适用于字符串。什么样的普通代码可以用于blob?

使用
缓冲区而不是
字符串应该可以,就像这样

http.createServer(function (request, response) {
    var body = Buffer.from([]); // create a buffer

    request.on('data', function (data) {
        // add to buffer
        body = Buffer.concat([body,data]);
        // Too much POST data, kill the connection!
        // 1e6 === 1 * Math.pow(10, 6) === 1 * 1000000 ~~~ 1MB
        if (body.length > 1e7) {
            console.log("POSTED data too large!")
            request.connection.destroy();
        }
    });
    request.on('end', function () {
        var pathname = "test.png";
        fs.writeFileSync(pathname, body, {flag: "w"});

        response.writeHead(200, {
              'Content-Type': 'text/plain',
              'Access-Control-Allow-Origin' : '*',
              // note: I had to add these because of the OPTIONS request
              'Access-Control-Allow-Headers' : 'Content-Type',
              'Access-Control-Allow-Methods' : 'GET,PUT,POST,DELETE,OPTIONS'
        });
        response.end("got it")
    });

}).listen(8888);
当我试着测试你的代码时,我得到了一个选项预检——上面的代码“处理”了它,但充其量它是黑客的——因为你似乎没有选项预检(因为你没有在代码中处理它),我想这只是我对你的代码做了一些错误的事情


可能有一种更好的方法可以将数据添加到缓冲区中-我已经有一段时间没有用node做过类似的事情了

使用
缓冲区
,而不是
字符串
应该可以工作,就像这样

http.createServer(function (request, response) {
    var body = Buffer.from([]); // create a buffer

    request.on('data', function (data) {
        // add to buffer
        body = Buffer.concat([body,data]);
        // Too much POST data, kill the connection!
        // 1e6 === 1 * Math.pow(10, 6) === 1 * 1000000 ~~~ 1MB
        if (body.length > 1e7) {
            console.log("POSTED data too large!")
            request.connection.destroy();
        }
    });
    request.on('end', function () {
        var pathname = "test.png";
        fs.writeFileSync(pathname, body, {flag: "w"});

        response.writeHead(200, {
              'Content-Type': 'text/plain',
              'Access-Control-Allow-Origin' : '*',
              // note: I had to add these because of the OPTIONS request
              'Access-Control-Allow-Headers' : 'Content-Type',
              'Access-Control-Allow-Methods' : 'GET,PUT,POST,DELETE,OPTIONS'
        });
        response.end("got it")
    });

}).listen(8888);
当我试着测试你的代码时,我得到了一个选项预检——上面的代码“处理”了它,但充其量它是黑客的——因为你似乎没有选项预检(因为你没有在代码中处理它),我想这只是我对你的代码做了一些错误的事情


可能有一种更好的方法可以将数据添加到缓冲区中-我已经有一段时间没有用node做过类似的事情了

此代码仅适用于字符串
-尝试使用Blob时会发生什么?哪一端失败了?它生成一个字符串而不是一个blob对象。上面的代码显示了一个名为body的字符串。数据被附加到其中,从而创建一个更大的字符串。
此代码仅适用于字符串
-当您尝试使用blob时会发生什么?哪一端失败了?它生成一个字符串而不是一个blob对象。上面的代码显示了一个名为body的字符串。数据被附加到它之后,创建了一个更大的字符串。对于这个项目,我将毫不留情地处理飞行前和一般CORS。至于缓冲区,谢谢!这段代码非常有效。我认为唯一值得更改的是缓冲区的实例化
new Buffer()
已被弃用以取代
Buffer。从([])
开始,对于这个项目,我将毫不留情地处理飞行前和一般COR。至于缓冲区,谢谢!这段代码非常有效。我认为唯一值得更改的是缓冲区的实例化
new Buffer()
已被弃用以取代
Buffer.from([])