File upload and sending data to backend using angular js

File upload and sending data to backend using angular js

In this Post We Will Explain About is File upload and sending data to backend using angular js With Example and Demo.Welcome on Pakainfo.com – Examples, The best For Learn web development Tutorials,Demo with Example! Hi Dear Friends here u can know to javascript – Angularjs $http post file and form data Example

In this post we will show you Best way to implement Uploading files and JSON data in the same request with Angular JS, hear for Post JSON data And Files In Same Request With AngularJS with Download .we will give you demo,Source Code and examples for implement Step By Step Good Luck!.

File Upload using Angularjs and Spring-MVC Restful service

In this Example,First of all Add or Inluce External Libs Like as a(jQuery, css etc..), and then create a simple index.php or index.html page.After that crate a simple javascript file like as a index.js or main.js, It is also add your web-application First Header Part to some priorty set.After that Include your relavant CSS Class.

Also Read :  Laravel Application Directory Structure Example

Here we are going to Learn about step by step uploading a file and all the data sending to (Server side)backend.

HTML(index.html)

<form name="upload" class="form">
  
 <button type="submit">Upload File</button>
</form>

Controller(index.js)

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

Here FileUploadServices is simple nothing but my any factory file .AS simple per structure based MVC standard it would be some all the operations good handling all data manipulation in angular factory file or angular service file.

$scope.upload_dataFile = function() {
 FileUploadServices.uploadfile($scope.files,
   function( msg ) //some generated success message
   {
    console.log('uploaded');
   },
   function( msg ) //some generated error
   {
    console.log('error');
   });
}

Factory file

uploadfile : function(files,success,error){
 
 var liveurl = 'http://www.Pakainfo.com/w3free/';

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

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

Good Luck Your file is very successfully uploaded

Also Read :  Custom Global Helpers in Laravel 5.8

Upload a file and send data and file to backend

If We want to All file send data(From serever side) along with simple file upload with angular We can try some below source code.There is a simple slight simple change while server side sending data

uploadfile : function( files,success, error )
{

 var fileData = new FormData();

 var liveurl = 'http://www.Pakainfo.com/w3free/';

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

 //here set all the sample data
 var data ={
  name : name,
  type : type
 };

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

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

You are Most welcome in my youtube Channel Please shubscibe my channel. and give me feedBackMore Details……
Angularjs Example

Also Read :  Smooth Scroll to ID using jQuery

Example

I hope you have Got What is AngularJS File Upload using $http post and FormData And how it works.I would Like to have FeaeBack From My Blog(Pakainfo.com) readers.Your Valuable FeedBack,Any Question,or any Comments abaout This Article(Pakainfo.com) Are Most Always Welcome.

Leave a Reply

avatar
  Subscribe  
Notify of