2017-07-24 3 views
-1

Ich habe Vanilla-Code für den Körper einer Anfrage, aber es erstellt eine Zeichenfolge. Bis jetzt hat das für die meisten Dinge gut funktioniert, aber jetzt möchte ich einen Blob erhalten.Wie bekomme ich einen Blob mit Vanilla Node.js?

Zunächst einmal, der Code Ich habe jetzt:

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); 

Client Side:

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 
}); 

Die hier Problem ist, dass dieser Code nur für Strings funktioniert. Was ist ein Vanilla-Code, der für Blobs funktionieren würde?

+0

'Dieser Code funktioniert nur für Strings' - Was passiert, wenn Sie mit Blobs versuchen? welches Ende versagt? –

+0

Erzeugt eine Zeichenfolge anstelle eines Blob-Objekts. –

+0

Was ist das "es", das das tut? –

Antwort

1

mit Buffer anstelle ein string funktionieren soll, wie so

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); 

Wenn ich Ihren Code versucht zu testen, ich war immer ein Option Preflight - der Code über „Griffe“, aber es ist am besten Hacky - Da Sie kein OPTIONS-Preflight zu haben scheinen (weil Sie in Ihrem Code nicht damit umgehen), habe ich festgestellt, dass ich etwas falsch gemacht habe

Es gibt wahrscheinlich einen weitaus besseren Weg, um die Daten hinzuzufügen in den Puffer - Ich habe solche Sachen mit Knoten in einer Weile

nicht getan
+0

Für dieses Projekt verwalte ich Preflight und allgemeine CORS mit wildem Verzicht. Wie für den Puffer, danke! –

+0

dieser Code funktioniert gut. Das einzige, was ich denke, könnte sich ändern, ist die Instanziierung von Puffer. 'new Buffer()' wurde veraltet anstelle von 'Buffer.from ([])' https://nodejs.org/api/buffer.html#buffer_new_buffer_array –

+0

änderte den Code, ich wusste nicht, dass es veraltet war: p –

Verwandte Themen