I am using express 3.x and node 1.9 with mongo and gridfs. To upload files I used gridfrom and it is working fine in normal form submit but not working in ajax based form submit. I followed few site but no help. http://www.davidpirek.com/blog/nodejs-html5-file-uploader
var gridform = require('gridform'),
mongoose = require('mongoose'),
mongo,
db,
app;
exports.upload_file = function(req, res){
var conn = mongoose.createConnection();
conn.open('localhost', 'employee', 27017, function (){
db = conn.db;
mongo = mongoose.mongo;
gridform.db = db;
gridform.mongo = mongo;
if ('POST' == req.method){
var form = gridform();
form.parse(req, function(err, fields, files) {
res.end("File ID:" + files.text.id);
});
}
});
}
The above code is working fine for normal form request. But not ajax form request. Below is my client side ajax call.
jQuery("form#someform").change(function(event){
var formData = new FormData();
formData.append("filePath", document.getElementById("filePath").files[0]);
var xhr = new XMLHttpRequest();
xhr.addEventListener("load", function (evt) {
var obj = jQuery.parseJSON(evt.target.responseText);
window.location.hash = 'File';
MVC.message.show({text: obj.message, hideDealy: 2000});
}, false);
xhr.open("POST", "/upload_file");
xhr.send(formData);
});