Upload multiple files using Angular Mongodb Nodejs

ANGULAR

//inject angular file upload directives and services.
var app = angular.module('fileUpload', ['ngFileUpload']);

app.controller('MyCtrl', ['$scope', 'Upload', '$timeout', function ($scope, Upload, $timeout) {
$scope.uploadFiles = function (files) {
$scope.files = files;
if (files && files.length) {
Upload.upload({
url: 'https://angular-file-upload-cors-srv.appspot.com/upload',
data: {
files: files
}
}).then(function (response) {
$timeout(function () {
$scope.result = response.data;
});
}, function (response) {
if (response.status > 0) {
$scope.errorMsg = response.status + ': ' + response.data;
}
}, function (evt) {
$scope.progress =
Math.min(100, parseInt(100.0 * evt.loaded / evt.total));
});
}
};
}]);

HTML

Upload on file select

Files:

  • {{f.name}}


{{errorMsg}}

NODEJS

var multipartyc = require('connect-multiparty'), multipartyMiddlewarec = multipartyc({uploadDir: '/var/www/html/workfromhome/atachedfiles'});

app.post('/uploadfilerequirements', multipartyMiddlewarec, function (req, res) {

//var stra = req.files.picturea.path;
// var pictureapath = stra.substring(stra.lastIndexOf("/") + 1, stra.length);

var arraystringfilestosave=" xxxx";

//console.log(req.files.files[0].path);

//

var i;
for (i = 0; i < req.files.files.length; i++) { var stra=req.files.files[i].path; var filepath = stra.substring(stra.lastIndexOf("/") + 1, stra.length); arraystringfilestosave += filepath+"#"; } console.log(arraystringfilestosave); var current_millies = new Date().getTime(); MongoClient.connect(url, function (err, db) { if (err) throw err; var dbo = db.db("workfromhome"); const uuidv1 = require('uuid/v1'); var contractuid = uuidv1(); var newcontract = {uid:contractuid, emailserviceprovider:req.body.emailserviceprovider }; dbo.collection('contracts').insertOne(newcontract, function (err, result) { if (err) { res.send('Error'); } else { res.send('true'); } }); }); });

Mongodb nodejs how to update nested array object

app.post('/openmessage', function (req, res) {

MongoClient.connect(url, function (err, db) {
if (err) throw err;

var dbo = db.db("workfromhome");

MongoClient.connect(url, function (err, db) {
if (err) throw err;

dbo.collection('users').updateOne({'messages.uid':req.body.uid},{'$set': {
'messages.$.readed': 'yes'
}}, function (err, result) {

if (err) {
console.log(err);
res.send('error');
db.close();
} else {
console.log("success");
res.send('success');
db.close();
}
});
});
});
})