Pano Pano - 2 months ago 8
Javascript Question

where is object stored in xmlhttprequest

Javascript:

var test_request = new XMLHttpRequest();
test_request.open("POST","/testupload",true);
test_request.send(image.sourceFile);


Node.js express:

app.post("/testupload", function(req, res){
where is the image.sourceFile in req?
});


where is the image.sourceFile in req? req.body shows {}, how can I get the image?

Answer

if you want to the form data of a POST request, you should try to use body-parser middleware first.

const express        = require('express');
const bodyParser     = require('body-parser');
const app            = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true, type: 'application/x-www-form-urlencoded' }));

now you can get form data in req.body;