Save file sent in Ajax in the backend node

1

Hello I have a backend on node 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){

  }
});

Can someone help me to know how I get the file in the backend and saved in a folder ...

    
asked by anonymous 20.07.2017 / 00:42

1 answer

0

Try to use something like 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!");
        });
    }
    // ...
});
    
20.07.2017 / 01:08