Radex
Radex

Reputation: 8587

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

Upvotes: 4

Views: 2231

Answers (2)

Jakub Pastuszuk
Jakub Pastuszuk

Reputation: 1038

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

Upvotes: 1

Mukesh Sharma
Mukesh Sharma

Reputation: 9022

You can get the path of file saved on local filesystem, by files[fileInputName][0].path

Upvotes: 0

Related Questions