Using contentType: false
solved many problems for me , just for the people who were looking for an answer to the same question , here is working and updated code.
Client Side Js:-
submitHandler: function (form) {
if(isClicked) {
$('#result').removeClass('alert-success').addClass('alert-error').html("Ticket Already submitted").show();
return false;
}
isClicked = true;
var data = new FormData();
data.append('messageData', $('#contact-form').serialize());
var filesList = document.getElementById('files');
for (var i = 0; i < filesList.files.length; i ++) {
data.append('file', filesList.files[i]);
}
$.ajax({
url: '/api/contactus/',
data: data,
processData: false,
type: 'POST',
contentType: false,
success: function (data) {
$('#contact-form').html('<p><span>Thank You for contacting us!</span><br> We will get back to you very soon.<br> Your Ticket No. is <strong>'+data.TokenId+'</strong> </p><br><button class="btn btn-primary close-button" type="submit">Submit Another Ticket</button>');
},
error: function (err) {
var errorMsg = 'Issue Submission Failed ! ' + err.statusText;
$('#result').removeClass('alert-success').addClass('alert-error').html(errorMsg).show();
}
});
}
Server Side:-
Nothing special was done at the server end of email sending, only attachments were handled in more generic way.
attachments: Attachments.map(function (f) {
return {
fileName: f.name,
filePath: f.path,
cid: f.path
};
})
Also this code was useful to delete those temp files from your temp folder after attachments are mailed,
/* Clean up the temporary files. */
Attachments.forEach(function (f) {
fs.unlinkSync(f.path);
});
and that's how was email sending code was called,so that BodyParser() and multipart() both could be used.
app.post('/api/contactus/', express.multipart(), api_calls.contactUs);