We want to implement a functionality to upload multiple files into ExpressJS server and return all its unique urls, by an ajax
call.
Following is the sample code in my front end:
var formData = new FormData();
for (var i = 0; i < nameId.length; i++) {
if($(nameId[i])[0].files[0]){
formData.append(nameId[i], $(nameId[i])[0].files[0], $(nameId[i])[0].files[0].name);
}
}
$.ajax({
url: '/upload-files',
type: 'POST',
data: formData,
processData: false,
contentType: false,
success: function(data){
console.log('upload successful!');
console.log(data);
}
});
And in our router we have following code snippet to accept the request and store the file:
router.post('/upload-files',function(req, res, next){
var form = new formidable.IncomingForm();
form.multiples = true;
form.uploadDir = path.join(__dirname, '/uploads');
form.on('file', function(field, file) {
console.log("File incoming");
fs.rename(file.path, path.join(form.uploadDir, file.name));
});
form.on('error', function(err) {
console.log('An error has occured: \n' + err);
});
form.on('end', function() {
res.end('success');
});
});
But, nothing ever happened in the router. Request is coming inside the router but after that nothing.
Is there anything wrong here? We are not getting any error in server side and in client side after long wait request is failing.
Please suggest. Thanks