Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete file after upload using node?

I am using multiparty to upload some file on the server, I have notice that when using form.parse a file is being added in temp in SO file system.

I need to remove that file after form close but I cannot get information of file path.

Any idea how to solve this problem?

function onUpload(req, res) {
  var form = new multiparty.Form();

  form.parse(req, function(err, fields, files) {
    onSimpleUpload(fields, files[fileInputName][0], res);
  });

  // Close emitted after form parsed
  form.on('close', function() {
    // cannot get file here to be deleted
  });
}
like image 221
Radex Avatar asked Jan 21 '26 05:01

Radex


1 Answers

To be specific:

var fs = require('fs');

var filePath = files[fileInputName][0].path;
fs.unlinkSync(filePath);

or async:

var fs = require('fs');

var filePath = files[fileInputName][0].path;
fs.unlink(filePath, function(err){
  if(err) // do something with error
  else // delete successful
});
like image 156
Jakub Pastuszuk Avatar answered Jan 23 '26 21:01

Jakub Pastuszuk