Save uploaded file to Ajax in backend Node

Asked

Viewed 128 times

1

Hello I have a backend in Ode and I’m getting this ajax request

var arquivo = $("#assinatura");
arquivo.on('change', function (event) {
  if (arquivo[0].files.length == 0)
    return false;

var data = new FormData();
data.append('assinatura', arquivo[0].files[0]); 
console.log(data);
$.ajax({
    url: "/salvarAssinatura",
    data: data,
    contentType: false,
    processData: false,
    type: "POST",
    success: function(data){

  }
});

Could anyone help me know how I get the file in backend and saved in a folder...

1 answer

0

Try using something similar to the code below.

var path = require('path'),
    fs = require('fs');
// ...
app.post('/salvarAssinatura', function (req, res) {
    var tempPath = req.files.file.path,
        targetPath = path.resolve('./uploads/image.png');
        fs.rename(tempPath, targetPath, function(err) {
            if (err) throw err;
            console.log("Upload completed!");
        });
    }
    // ...
});
  • I used multer, but thanks for the tip.

Browser other questions tagged

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