简体   繁体   中英

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

您可以通过files[fileInputName][0].path获得保存在本地文件系统上的文件的路径。

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM