Created
January 11, 2014 09:42
-
-
Save AmarPrabhu/8368895 to your computer and use it in GitHub Desktop.
Form upload using Node Formidable
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@azherullahkhan this example would upload to File system.