Skip to content

Instantly share code, notes, and snippets.

@backsapce
Created September 26, 2017 01:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save backsapce/3ef96e120894cc68fc0b3e943bfc6e31 to your computer and use it in GitHub Desktop.
Save backsapce/3ef96e120894cc68fc0b3e943bfc6e31 to your computer and use it in GitHub Desktop.
formidable upload file for express
app.post('/uploadFile',function (req,res) {
// create an incoming form object
var form = new formidable.IncomingForm();
// specify that we want to allow the user to upload single files in a single request
form.multiples = false;
//keep the file upload extensions
form.keepExtensions = true;
// 500M limit
form.maxFieldsSize = 500 * 1024 * 1024;
// store all uploads in the /uploads directory
form.uploadDir = path.join(__dirname, './tmp');
// every time a file has been uploaded successfully,
// rename it to it's orignal name
form.on('file', function(field, file) {
//go for you code
});
// log any errors that occur
form.on('error', function(err) {
console.error('An error has occured: \n' + err);
});
// once all the files have been uploaded, send a response to the client
form.on('end', function() {
res.end('success');
});
// parse the incoming request containing the form data
form.parse(req);
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment