How to save picture blob with multer in nodejs

Asked

Viewed 345 times

0

Well I have this table:

inserir a descrição da imagem aqui

And I have this code that I get an image by the woman in the nodejs:

routes.post('/salvarImagem', upload.single('Img') , (req, res) => {
    const connection = mysql.createConnection({
        host     : 'localhost',
        port     : 3306,
        user     : 'root',
        password : '',
        database : 'jubart'
    });

    var query = "INSERT INTO tb_imagens (FK_Portifolio, Imagem, Nome, Tipo, Tamanho) VALUES (" + req.body.Portifolio + ", BINARY(" + req.file.buffer.toString('binary') + "), '" + req.file.originalname + "', '" + req.file.mimetype + "', '" + req.file.size + "')";

    connection.query(query, function(error, results, fields){
        if(error) 
            res.json(error);
        else{
            res.json(results);
        }
        connection.end();
    });
});

It saves but the image it saves wrong it does not save as binary file but rather a file with Base64 written and when I return from the database I can not display the image. Does anyone know how I convert req.file.buffer to blob format for saving?

  • Ever tried to replace BINARY(" + req.file.buffer.toString('binary') + ") for " + req.file.buffer + ")?

  • Yes and so n salva

No answers

Browser other questions tagged

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