I am using formidable
node module for uploading the file.
Here is my .jade code
form#uploadForm(enctype='multipart/form-data', method='POST', action='/upload')
input#uploadTestSheet(type='file', name='uploadTestSheet', accept='.csv, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel')
button(type='submit') Submit
The files are getting uploaded but if a file already exists in the directory and then a user is trying to add a file with the same name then the new file is not getting uploaded. Here is my Server
var fs = require('node-fs-extra');
var formidable = require('formidable');
app.use(bodyparser({defer: true}));
app.post('/upload',function (req, res, next) {
var form = new formidable.IncomingForm();
form.uploadDir =__dirname +"/uploads";
form.keepExtensions = true;
form.parse(req, function(err, fields, files) {
fs.rename(files.uploadTestSheet.path,__dirname +'/uploads'+files.uploadTestSheet.name, function(err) {
if (err){
res.render('ManualMode',{data:{id:req.user.id, hash:req.user.hash, nodePollInterval:req.user.nodePollInterval}});
console.log("cannot upload: "+err);
}
});
res.render('index',{data:{id:req.user.id, hash:req.user.hash, nodePollInterval:req.user.nodePollInterval}});
});
});