0
I am performing a test to implement an image upload and created the following code in the back end in Node js.
function uploadImagem (req, res) {
var restauranteId = req.params.id;
var file_name = 'No subido...';
if(req.files){
var file_path = req.files.image.path;
var file_split = file_path.split('\\');
var file_name = file_split[2];
res.status(200).send({
file_path: file_path,
file_split: file_split,
file_name: file_name
});
}else{
res.status(200).send({message: 'não a solicitação de usuário'});
}
}
When I submit the data it only falls into the condition below;
else{
res.status(200).send({message: 'não a solicitação de usuário'});
}
The expected result was to be similar to this;
How to get around this situation?
Realizing the suggestion of @Thiago Magalhães the method was like this;
function uploadImagem (req, res) {
res.status(200).send({message: req.body.fles});
var restauranteId = req.params.id;
var file_name = 'No subido...';
if(req.files){
var file_path = req.files.image.path;
var file_split = file_path.split('\\');
var file_name = file_split[2];
res.status(200).send({
file_path: file_path,
file_split: file_split,
file_name: file_name
});
}else{
res.status(200).send({message: 'não a solicitação de usuário'});
}
}
I have no way to use the console.log()
because it’s a back-end test, so I used the res.status(200).send({message: req.body.fles});
, I also tested with the res.status(200).send({message: req.body.image});
But the two result give {}
empty.
see if the image is not going to
req.body.image
orreq.body.files
– Thiago Magalhães
How do I know ?
– wladyband
puts these two options I spoke within a
console.log()
at the very beginning of its functionuploadImagem
.. and see in the console if you find the information of the sent image– Thiago Magalhães
@Thiagomagalhães I did an update on the post take a look please.
– wladyband
You are giving an empty value in both cases
{}
– wladyband
I put the
console.log(req.files);
before the if and the result wasundefined
at MSDOS, why is this happening?– wladyband
So that way you’ll have to nail it like this example. I particularly prefer to use the multer, just create a middleware before the request.
– Chance
I found it very complicated :/
– wladyband
With multer is very simple, with 3 lines you upload the file.
– Chance