Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to upload files using Angular JS

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 356
    Comment on it

    Hello Readers if you want to upload files (images or music) then now Angular JS offers you a way lot more esier. Using angular give user a light and fast interface which is very useful for low internet speed connections.

    Lets see the working example of this:- You have to create a file with name file.html and paste the following code in it.

    <html>   
       <head>
          <script src = "http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
       </head>       
       <body ng-app = "myApp">        
          <div ng-controller = "myCtrl">
             <input type = "file" file-model = "myFile"/>
             <button ng-click = "uploadFile()">upload me</button>
          </div>          
          <script>
             var myApp = angular.module('myApp', []);
    
             myApp.directive('fileModel', ['$parse', 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]);
                         });
                      });
                   }
                };
             }]);          
             myApp.service('fileUpload', ['$http', function ($http) {
                this.uploadFileToUrl = function(file, uploadUrl){
                   var fd = new FormData();
                   fd.append('file', file);                
                   $http.post(uploadUrl, fd, {
                      transformRequest: angular.identity,
                      headers: {'Content-Type': undefined}
                   })                
                   .success(function(){
                   })                
                   .error(function(){
                   });
                }
             }]);
    
             myApp.controller('myCtrl', ['$scope', 'fileUpload', function($scope, fileUpload){
                $scope.uploadFile = function(){
                   var file = $scope.myFile;                   
                   console.log('file is ' );
                   console.dir(file);                   
                   var uploadUrl = "/fileUpload";
                   fileUpload.uploadFileToUrl(file, uploadUrl);
                };
             }]);                
          </script>
    
       </body>
    </html>
    

    In the code above code myApp.directive will show your the name of file and onclicking the uplod button this script will upload files in background. There will be no refresh or redirect.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: