2016年4月25日 星期一

multipart/form-data File Upload with AngularJS


https://uncorkedstudios.com/blog/multipartformdata-file-upload-with-angularjs



angular.module('yoApp')

//uploadService
  .service('fileUpload',function ($q,$http) {

    this.uploadFileToUrl = function(file, uploadUrl){

        var deferred = $q.defer();

        var fd = new FormData();
        fd.append('file', file);
        $http.post(uploadUrl, fd, {
            transformRequest: angular.identity,
            headers: {'Content-Type': undefined}
        })
        .success(function(result){
        console.log(result);
            deferred.resolve(result);
        })
        .error(function(error){
        console.log(error);
            deferred.reject(error);
        });

        return deferred.promise;
    }
  })
  .directive('fileModel',function ($parse) {
    return {
        restrict: 'A',
        link: function(scope, element, attrs) {
            var model = $parse(attrs.fileModel);
            var modelSetter = model.assign;
         
            element.bind('change', function(){
                scope.$apply(function(){
                    modelSetter(scope, element[0].files[0]);
                });
            });
        }
    };
  })
  .controller('uploadCtrl', function ($scope,fileUpload,$q,$timeout,buildingIndelFactory) {

    $scope.myFile;

    $scope.uploadFile = function(type,label){

        var file = $scope.myFile;
        var uploadUrl = "/upload";

   $q.all([
           fileUpload.uploadFileToUrl(file, uploadUrl)
       ]).then(function(value) {

   }, function(reason) {

   });
     
    };
 

  });

2016年4月14日 星期四

2016年4月13日 星期三

mongo note



login as Admin
mongo -u peter -p Asdf12345678 --authenticationDatabase admin


Delete whole table (collection)
db.ShareCode.drop()


Create Admin
sudo mongo admin --username root --password PASSWORD
show dibs
use admin
db.createUser
dbcreateUser({
user: "USERNAME",
pwd: "PASSWORD",
roles: ["root"]
})

mongo -u peter -p Asdf12345678 --authenticationDatabase admin


mongodump --host 23.99.110.206 --port 27017 -u peter -p Asdf12345678 --out data/backup/

Backup By mongodump
mongodump --host localhost --port 27017 -u peter -p Asdf12345678 --out data/backup/

Restore By mongorestore
mongorestore --host localhost --port 27017 -u peter -p Asdf12345678 data/backup
sudo service mongod start


Open extended connect



command:



db
use dbNAME
show dbNAME
show dbs
show tables
db.TABLES.find()
db.TABLES.find({"text":"abc"})
db.collectionsName.find()

Remove Data Directories.

sudo rm -r /var/log/mongodb
sudo rm -r /var/lib/mongodb


-The MongoDB instance stores its data files in /var/lib/mongodb 
-its log files in/var/log/mongodb by default
-runs using the mongodb user account. 
-You can specify alternate log and data file directories in /etc/mongod.conf


Error 
Failed global initialization: BadValue Invalid or no user locale set. Please ensure LANG and/or LC_* environment variables are set correctly.

Answer 
export LC_ALL=C

2016年4月8日 星期五

publish npm module, package

from: https://docs.npmjs.com/getting-started/publishing-npm-packages

Publish

npm init

....when finishing your project

npm publish



Update version

npm version patch

npm publish

little Update

npm version minor

npm publish

types: patch, minor, or major
Test: Go to https://npmjs.com/package/<package>. The package number should be updated.

2016年4月6日 星期三

nodejs, host, web, simple, connect, serve-static


npm install -save connect serve-static



app.js

var connect = require('connect'),
    serveStatic = require('serve-static');

var app = connect();

app.use(serveStatic("./"));
app.listen(3000);


node app

2016年4月5日 星期二

redis , nodejs

Redis是一個開源、支援網路、基於記憶體、鍵值對儲存資料庫,使用ANSI C編寫
Redis is an open source (BSD licensed), in-memory data structure store, used as database, cache and message broker.

nodejs 不提供健立 redis server 功能,目前只有 client side lib 連接到 redis server

安裝