I am using express and ajax to upload an image file to server disk. My express code looks like
app.post('/events/upload',function(req,res){
upload(req,res,function(err) {
if(err) {
return res.end(err);
}
console.log("333333333333333333333333333")
res.send("File uploded");
});
});
My ajax code is :
$(function () {
$('#upload-form').click(function (e) {
e.preventDefault();
console.log("$$$$$$$$$$$$$$$$$$$$$$$$$");
$.ajax({
url: '/events/upload',
type: 'POST',
success: function () {
console.log("Uploded");
},
error: function () {
console.log("Not uploded");
}
});
})
})
and my html is
<form method='post' action='upload' enctype="multipart/form-data">
<input id="upload" type="file" class="btn btn-warning" name="userPhoto"/>
<input id="upload-form" type="submit">
</form>
Now, when i remove the preventDefault the image file is being uploded and redirected to "File upload", However, when the preventDefault is there, every logs is being printed like "33333333333" and "$$$$$$$$$$$$$" but the file is not being uploded. Any idea why is this happening.