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?
'Dieser Code funktioniert nur für Strings' - Was passiert, wenn Sie mit Blobs versuchen? welches Ende versagt? –
Erzeugt eine Zeichenfolge anstelle eines Blob-Objekts. –
Was ist das "es", das das tut? –