Angularjs 如何使用angular将图像发布到Web API?

Angularjs 如何使用angular将图像发布到Web API?,angularjs,Angularjs,这是我在html模式中的内容: <div id="name-group" class="form-group"> <label>Upload image *</label> <input type="file" name="img" class="form-control" ng-model="formData.img" > </div>` 在你们工厂的控制器里,我会写: s.factory('api', ['$q',

这是我在html模式中的内容:

<div id="name-group" class="form-group">
    <label>Upload image *</label>
    <input type="file" name="img" class="form-control" ng-model="formData.img" >
</div>`

在你们工厂的控制器里,我会写:

s.factory('api', ['$q', '$http', function ($q, $http) {
 return {
  upload: function (file) {
     post: function () {
        var deferred = $q.defer();
       $http.post(api_url+'/artwork/cause', file).then(function(response){
            deferred.resolve(response);
        }, function (error) {
            deferred.reject(error);
        });
        return deferred.promise;
    }
  }
}
在控制器中,您可以调用api.upload方法:

s.controller('apiCtrl', ['$scope', 'api', function($scope, api){
    $scope.upload = function(file) {
        api.upload(file).then(function(response){
            // Do what you want to with the response    
        })
    }
}])
s.controller('apiCtrl', ['$scope', 'api', function($scope, api){
    $scope.upload = function(file) {
        api.upload(file).then(function(response){
            // Do what you want to with the response    
        })
    }
}])