I'm writing this web-page in angularJS where I want people to edit and store text and images. I've created a file uploading function that let's you upload files from the users computer. The problem is getting this file stored into mongoDB. I've read alot of examples on gridFS but none of them quite matched what I'm trying to do. Here's my code:
web-server.js:
app.post('/uploadFile', function(req,res){
console.log("Retrieved:");
console.log(req.files);
var Grid = require('gridfs-stream');
var gfs = Grid(DB, mongoose.mongo);
// streaming to gridfs
var writestream = gfs.createWriteStream(req.files.file);
fs.createReadStream(req.files.file.path).pipe(writestream);
services.js:
function uploadFilesToServer(file){
var fd = new FormData();
fd.append("file", file);
var deferred = $q.defer();
console.log("trying to save:");
console.log(file);
$http({
method:"POST",
url: "uploadFile",
data: fd,
withCredentials: true,
headers: {'Content-Type': undefined },
transformRequest: angular.identity
}).success(function(data){
var returnValue = [true, file, data];
deferred.resolve(returnValue);
}).error(function(data){
var returnValue = [false, file, data];
deferred.resolve(returnValue);
});
return deferred.promise;
}
At the moment I'm not getting any error messages when I run the code, but neither is the images stored in the db.files or db.chunks. Any help is appreciated.