Angular JS image upload service

 
$scope.myFile = $scope.files[0];  
 var file = $scope.myFile;   
 var url = "../api/upload.php";  
 uploadFile.uploadFiletoServer(file, url, title, category, content, titleid, istitle, links);  
  $scope.uploadedFile = function(element) {  
       var reader = new FileReader();  
       reader.onload = function(event) {  
        $scope.$apply(function($scope) {  
         $scope.files = element.files;  
          $scope.src = event.target.result   
        });  
       }  
  reader.readAsDataURL(element.files[0]);  
  }  
 app.service('uploadFile', ['$http', function ($http) {  
           this.uploadFiletoServer = function(file, uploadUrl, title, category, content, titleid, istitle, links){  
                 var fd = new FormData();  
                 fd.append('file', file);  
                 fd.append('title', title);  
                 fd.append('category', category);  
                 fd.append('titleid', titleid);  
                 fd.append('istitle', istitle);  
                 fd.append('links', links);  
                 $http.post(uploadUrl, fd, {  
                      transformRequest: angular.identity,  
                      headers: {'Content-Type': undefined,'Process-Data': false}  
                 })  
                 .success(function(data){  
                      var filename = data.trim();  
                      $http.post('../api/insert.php',{'filename':filename,'title':title,'category':category,'content':content,'titleid':titleid,'istitle':istitle,'links':links})  
                      .success(function(result){  
                      });  
                 })  
                 .error(function(){  
                     //alert("Error");  
                 });  
            }  
 }]);  
     ///////////////// upload.php ////////////////////////  
      if(!empty($_FILES['file'])){  
           $ext = pathinfo($_FILES['file']['name'],PATHINFO_EXTENSION);  
           $image = time().'.'.$ext;  
           move_uploaded_file($_FILES["file"]["tmp_name"], 'images/'.$image);  
        echo $image;  
     }  
Previous
Next Post »