I have created a service for uploading images to a server, and I am encountering an issue when calling this service from my controller. The error message indicates that the function 'uploadFileToUrl' is undefined.
Below is the code for my service:
app.service('fileUpload', ['$http', function ($http) {
this.uploadFileToUrl = function(file, clientId, uploadUrl){
var fd = new FormData();
fd.append('file', file);
fd.append('id', clientId);
$http.post(uploadUrl, fd, {
transformRequest: angular.identity,
headers: {'Content-Type': undefined}
})
.then(function(response){
console.log(response.data);
}, function(error){
console.error("Error occurred during file upload");
});
}
}]);
And here is how I am using this service in my controller:
app.controller('settingsCtrl', ['$scope','apiCall','$filter', function($scope, apiCall, $filter, fileUpload){
$scope.saveStudioBranch = function(){
studio();
admin();
branch();
}
function studio(){
var studioData = {"session_id": session_id, "u":[{"col":"studio","val":$scope.studioDetails.studio},{"col":"type","val":$scope.studioDetails.type}],"filter":[["id","=", "1"]], "table": "studio"};
apiCall.callEndpoint(studioData,"settingRoute.php","update",json_header).then(function(response){
if(response.data.error == 0){
if($scope.inst_logo !== undefined ){
var uploadUrl = "https://papa.fit/routes/registrationRoute.php?action=instLogo";
fileUpload.uploadFileToUrl($scope.inst_logo, session.client_id, uploadUrl);
}
} else {
show_snack(response.data.message);
}
});
}
});
An arrow has been added where the service is being called from.