Skip to content

Instantly share code, notes, and snippets.

@AmarPrabhu
Created January 11, 2014 09:42
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 AmarPrabhu/8368895 to your computer and use it in GitHub Desktop.
Save AmarPrabhu/8368895 to your computer and use it in GitHub Desktop.
Form upload using Node Formidable
exports.fileUpload = function(req, res) {
var form = new formidable.IncomingForm(),
files = [],
uploadedFilePath,
fields = [];
form.uploadDir = '/your_path/files';
form.keepExtensions = true;
form.on('field', function(field, value) {
fields.push([field, value]);
})
form.on('file', function(field, file) {
console.log(file.name);
console.log(file.path);
uploadedFilePath = file.path;
//fs.rename(file.path, form.uploadDir + "/" + file.name);
//files.push([field, file]);
})
form.on('end', function() {
//res.redirect('/forms');
fs.readFile(uploadedFilePath, 'base64', function(err, data) {
if (err) {
console.log(err);
res.send(500);
}
var Magic = mmm.Magic;
var magic = new Magic(mmm.MAGIC_MIME_TYPE);
magic.detectFile(uploadedFilePath, function(err, result) {
if (err) throw err;
console.log(result);
//doyour stuff with file
});
});
});
form.parse(req);
};
@AmarPrabhu
Copy link
Author

@azherullahkhan this example would upload to File system.

@AmarPrabhu
Copy link
Author

@azherullahkhan
Copy link

Thanks a ton Amar!! Apologize for the delay in my response.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment