I'm using formidable to upload file with nodejs . I want change directory by type of file . I.e : img file to folder img , video to folder video ,.. Here is my code
var form = new formidable.IncomingForm();
var fileName = req.body.file;
console.log(fileName); //log successfully file name
if(fileName.match(/\.(jpg|jpeg|png)$/i)){
form.uploadDir = path.join(__dirname, '../public/uploads/img');
}else{
form.uploadDir = path.join(__dirname, '../public/uploads');
}
form.on('file', function(field, file) {
var d = new Date();
var t = d.getTime();
var newName = md5(file.name) + t;
fs.rename(file.path,path.join(form.uploadDir,newName),function(err) {
if(err)
console.log(err);
console.log('Success')
});
});
// log any errors that occur
form.on('error', function(err) {
console.log('An error has occured: \n' + err);
});
// parse the incoming request containing the form data
form.parse(req, function(err, fields, files) {
});
But I get error cannot read match of undefined in this line : if(fileName.match(/\.(jpg|jpeg|png)$/i))
. Where is my wrong ? Please help me
via Thanh Tùng
No comments:
Post a Comment