I want to send data back to my controller from service if the size of file extents how can i achieve it here is my code.
myApp.service('fileUpload', ['$http', function ($http) {
this.uploadFileToUrl = function (fileData, uploadUrl) {
if (fileData.size > 50000000) {
var fd = new FormData();
fd.append('file', fileData);
$http.post(uploadUrl, fd, {
transformRequest: angular.identity,
headers: { 'Content-Type': undefined }
})
.success(function () {
})
.error(function () {
});
}
else {
return "Image size is more than 5MB";
}
}
}]);
You should use $q service for deferred execution of request like as -
myApp.service('fileUpload', ['$http','$q', function ($http,$q) {
this.uploadFileToUrl = function (fileData, uploadUrl) {
if (fileData.size > 50000000) {
var fd = new FormData();
fd.append('file', fileData);
var deferred = $q.defer();
$http.post(uploadUrl, fd, {
transformRequest: angular.identity,
headers: { 'Content-Type': undefined }
})
.then(function (result) {
deffered.resolve(result);
},function(error) {
deffered.reject();
});
return deferred.promise;
}
else {
return "Image size is more than 5MB";
}
}
}]);
For more information,check below links-
https://docs.angularjs.org/api/ng/service/$q https://thinkster.io/a-better-way-to-learn-angularjs/promises