AngularJS File Upload using Ajax POST Form Web API

AngularJS File Upload using Ajax POST Form Web API

In this Post We Will Explain About is AngularJS File Upload using Ajax POST Form Web API With Example and Demo.

Welcome on live24u.com – Examples ,The best For Learn web development Tutorials,Demo with Example! Hi Dear Friends here u can know to AngularJS File Upload using $http post and FormData

In this post we will show you Best way to implement file upload using angularjs and web api, hear for How to javascript – AngularJs Ajax POST form with file upload with Download .we will give you demo,Source Code and examples for implement Step By Step Good Luck!.

Also Read :   List Of Top 10 Array Example using JavaScript

You can download source code and Demo from below link.




File upload and sending data to backend using angular js

Here we are simple going to know to about uploading(file, images, documents as…) a file and sending to (Like as a server side)backend using Angularjs.

Html

<h2></h2>
<hr/>
<form name="upload" class="form" data-ng-submit="createfile()">
  <input type="file" name="file" multiple 
 onchange="angular.element(this).scope().fileUploadData(this)" />
 <button type="submit">Upload </button>
</form>
<hr/>

Controller

$scope.fileUploadData = function(element) {
 $scope.$apply(function($scope) {
   $scope.files = element.files;         
 });
}

uploadservice simple is nothing but my new angular factory file .AS per simple MVC standard rules it would be good or very easy to handling all data some manipulation in like as a factory file or service file.

$scope.createfile = function() {
 UploadService.uploadfile($scope.files,
   function( msg ) // success
   {
    console.log('uploaded');
   },
   function( msg ) // error
   {
    console.log('error');
   });
}

Factory file

uploadfile : function(files,success,error){
 
 var apiurl = 'Like as a (www.live24u.com/api/list)your web service apiurl';

 for ( var i = 0; i < files.length; i++)
 {
  var filedata = new FormData();
 
  filedata.append("file", files[i]);
 
  $http.post(apiurl, filedata, {
  
   withCredentials : false,
  
   headers : {
    'Content-Type' : undefined
   },
 transformRequest : angular.identity

 })
 .success(function(data)
 {
  console.log(data);
 })
 .error(function(data)
 {
  console.log(data);
 });
}
}

success message display here like as a Your file is successfully uploaded

Also Read :   Codeigniter insert update delete - Codeigniter MVC CRUD

Upload a file and send data(file, images etc. ) and file to (server side)backend

If we simple want to send data all the server side along with file upload we can simple last step to try below source code .There is a very slight change some changes while sending data using Angularjs.

uploadfile : function( files,success, error )
{

 var filedata = new FormData();

 var apiurl = 'your web service apiurl';

 angular.forEach(files,function(file){
 filedata.append('file',file);
 });

 //fake data or any dta...sample data
 var data ={
  name : name,
  type : type
 };

 filedata.append("data", JSON.stringify(data));

 $http.post(apiurl, filedata, {
  withCredentials : false,
  headers : {
  'Content-Type' : undefined
  },
  transformRequest : angular.identity
 })
 .success(function(data)
 {
  console.log(data);
 })
 .error(function(data)
 {
  console.log(data);
 });
},

You can download source code and Demo from below link.




Also Read :   Dynamic Category Subcategory Tree using AngularJS

Example

I hope you have Got Uploading files and JSON data in the same request with Angular JS And how it works.I would Like to have FeadBack From My Blog(live24u.com) readers.Your Valuable FeadBack,Any Question,or any Comments abaout This Article(live24u.com) Are Most Always Welcome.