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();
}
});
});
});
})

MONGODB NODEJS increment in 1 integer row

app.get('/facebookads', function (req, res) {

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

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

dbo.collection('Facebookads').findOneAndUpdate({fbkads: "placementone"},{ $inc: { "click" : 1 } },function (err, result) {

if (err) {
console.log("entra a error")
res.send('error');
}
else if (!result) {
console.log("entra a notmatch")

res.send('notmatch');

} else {

console.log(result+" done");

res.send(result+" done");
//res.send(result);
}

db.close();

});
});
})

Unity JSON array with JsonParser from nodejs and mongodb

StartCoroutine(GetTotalClicks(PlayerPrefs.GetString ("email")));

IEnumerator GetTotalClicks(string email)
{
var url = "http://www.xxxxxxx.com:8088/gettotalclicks";
var form = new WWWForm();
form.AddField("email", email);

WWW www = new WWW(url, form);

yield return www;

if (www.error == "" || www.error == null) {

UnityEngine.Debug.Log(www.text);

string JSONToParse = "{\"Items\":" + www.text + "}";

Player[] player = JsonHelperb.FromJson(JSONToParse);

Debug.Log (player [0].clicks);
Debug.Log (player [1].clicks);
}
else
{
UnityEngine.Debug.Log("WWW Error: " + www.error);
}
}

[System.Serializable]
public class Player
{

public string _id;
public string email;
public string date;
public string clicks;
public string done;
public string deviceid;
public string eggfinished;

}

public static class JsonHelperb
{
public static T[] FromJson(string json)
{
Wrapper wrapper = JsonUtility.FromJson>(json);
return wrapper.Items;
}

public static string ToJson(T[] array)
{
Wrapper wrapper = new Wrapper();
wrapper.Items = array;
return JsonUtility.ToJson(wrapper);
}

public static string ToJson(T[] array, bool prettyPrint)
{
Wrapper wrapper = new Wrapper();
wrapper.Items = array;
return JsonUtility.ToJson(wrapper, prettyPrint);
}

[System.Serializable]
private class Wrapper
{
public T[] Items;
}
}

Nodejs Forever Stop a concrete process

forever list

and you will get:
------------------------------
info: Forever processes running
data: uid command script forever pid id logfile uptime
data: [0] 9Xzw ng serve --host 0.0.0.0 --port 4009 13164 29579 /home/ec2-user/.forever/9Xzw.log 7:1:20:50.412
data: [1] wOj1 npm run-script app-start-dev
-------------------------------

Then:

forever stop 1 (for the process on the 1 place of the array)

Nodejs upload picture / file with angular

NODE CODE

npm install connect-multiparty

var multiparty = require('connect-multiparty'), multipartyMiddleware = multiparty({ uploadDir: '/var/www/html/beetalk/myads/imagesb' });

var bodyParser = require('body-parser')
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));

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

//console.log(req.body, req.files);

res.send(req.files);

console.log(req.body, req.files);

});

ANGULAR / HTML

var app = angular.module("myApp", ["ngRoute", 'ngCookies','ngAnimate', 'ngSanitize', 'ui.bootstrap','ngFileUpload']);

app.controller('addnewad', function ($scope, $uibModalInstance, $http, $cookies, Upload) {

$scope.upload = function (file) {

Upload.upload({
url: 'https://www.beetalkweb.com:8443/addnewad',
data: {file: file, 'appname': "abc"}
}).then(function (resp) {
alert('Success ' + resp.config.data.file.name + 'uploaded. Response: ' + JSON.stringify(resp.data));
console.log('Success ' + resp.config.data.file.name + 'uploaded. Response: ' + resp.data);
}, function (resp) {
alert( console.log('Error status: ' + resp.status));
console.log('Error status: ' + resp.status);
}, function (evt) {
var progressPercentage = parseInt(100.0 * evt.loaded / evt.total);
console.log('progress: ' + progressPercentage + '% ' + evt.config.data.file.name);
});
};

$scope.ok = function () {

// alert($cookies.get('username'));
$scope.upload($scope.file);
// alert($scope.addnewad.appname+" "+$scope.addnewad.apppackage+" "+$scope.addnewad.appdescription)

$uibModalInstance.dismiss('cancel');

}
$scope.cancel = function () {
$uibModalInstance.dismiss('cancel');
};
});

Connect Angular with Nodejs using post and passing parameters using https

ANGULAR FILE

var app = angular.module('myApp', []);
app.controller('myCtrl', function($window,$scope, $http) {

// $http.defaults.headers.post["Content-Type"] = "application/x-www-form-urlencoded";

$scope.username="ferran";
$scope.password="1234";

$scope.loginb = function() {

// alert($scope.username+" "+$scope.password);
// $http.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=utf-8';
$http({
url: 'https://www.beetalkweb.com:8443/login',
method: "POST",
data: {username:$scope.username,password:$scope.password}
// headers: {'Content-Type': 'application/x-www-form-urlencoded'}

})
.then(function(response) {

if(response.data=="true"){

$window.location.href = 'https://www.beetalkweb.com/myads/dashboard.html'; //You should have http here.

}else{

$scope.content="Username and Password not match";

}

},
function(response) {

alert(response.data+" error");

$scope.content = response.data;

});

};

NODEJS FILE

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

MongoClient.connect(url, function(err, db) {
if (err) throw err;
var dbo = db.db("myads");

var query = {username: req.body.username, password: req.body.password };

dbo.collection('users').findOne(query, function (err, result) {
if (err){
res.send('error');
}
else if(!result){

// res.send(req.body.username+" "+req.body.password);
res.send('notmatch');

}else{
res.send('true');
//res.send(result);
}

db.close();

});
});
})

Connect Nodejs Mongodb and Angular with https

nodejs file

var fs = require('fs');
var https = require('https');
var privateKey = fs.readFileSync('certs/www.beetalkweb.com.key', 'utf8');
var certificate = fs.readFileSync('certs/bba86444d2e3bca1.crt', 'utf8');

var credentials = {key: privateKey, cert: certificate};
var express = require('express');
var app = express();

var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://Fespunaa:1982Fesp1982@127.0.0.1:27017/";

var bodyParser = require('body-parser')

app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});

app.get('/', function(req, res, next) {
// Handle the get for this route
res.render('index', {});

});

app.post('/', function(req, res, next) {
// Handle the post for this route
});

app.get('/conne', function (req, res) {

MongoClient.connect(url, function(err, db) {
if (err) throw err;
var dbo = db.db("myads");

dbo.collection("ads").find({"ad_name":"adname"}).toArray(function(err, result) {

if (err) throw err;

res.send(result);

db.close();
});
});

})

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

MongoClient.connect(url, function(err, db) {
if (err) throw err;
var dbo = db.db("myads");

dbo.collection("ads").find({"ad_name":"adname"}).toArray(function(err, result) {

if (err) throw err;

res.send(result);

db.close();
});
});

})

var httpsServer = https.createServer(credentials, app);
httpsServer.listen(8443);

Angular / html file


WeTalk



{{content}}


ver en editar texto innterior