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 infinityknow.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!.
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
[php]
[/php]
Controller
[php]
$scope.fileUploadData = function(element) {
$scope.$apply(function($scope) {
$scope.files = element.files;
});
}
[/php]
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.
[php]
$scope.createfile = function() {
UploadService.uploadfile($scope.files,
function( msg ) // success
{
console.log(‘uploaded’);
},
function( msg ) // error
{
console.log(‘error’);
});
}
[/php]
Factory file
[php]
uploadfile : function(files,success,error){
var apiurl = ‘Like as a (www.infinityknow.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);
});
}
}
[/php]
success message display here like as a Your file is successfully uploaded
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.
[php]
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);
});
},
[/php]
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(infinityknow.com) readers.Your Valuable FeadBack,Any Question,or any Comments abaout This Article(infinityknow.com) Are Most Always Welcome.