Image upload problem in nodejs

Asked

Viewed 32 times

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

I’m doing like this; inserir a descrição da imagem aqui

The expected result was to be similar to this;

inserir a descrição da imagem aqui

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 or req.body.files

  • How do I know ?

  • puts these two options I spoke within a console.log() at the very beginning of its function uploadImagem.. and see in the console if you find the information of the sent image

  • @Thiagomagalhães I did an update on the post take a look please.

  • You are giving an empty value in both cases {}

  • I put the console.log(req.files); before the if and the result was undefined at MSDOS, why is this happening?

  • 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.

  • I found it very complicated :/

  • With multer is very simple, with 3 lines you upload the file.

Show 4 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.