使用角度控制器中的文本框发送多个图像数据

时间:2017-09-16 06:31:07

标签: php angularjs angularjs-scope

我想将带有文本框值的多个图像数据发送到服务器端(PHP)。我已经完成了多个图像上传但是在提交表单时我无法将数据发送到服务器端。我的视图代码低于

<form ng-submit="save()"><input type="file" file-upload multiple>
<div ng-repeat="step in files">
    <img ng-src="{{step}}" />{{step.name}}
    <input type="text" ng-model="comments">
</div>
<input type="submit"></form>

在我的控制器中

app.directive('fileUpload', function() {
  return {
    scope: true, //create a new scope
    link: function(scope, el, attrs) {
      el.bind('change', function(event) {
        var files = event.target.files;
        //iterate files since 'multiple' may be specified on the element
        for (var i = 0; i < files.length; i++) {
          //emit event upward
          scope.$emit("fileSelected", {
            file: files[i]
          });
        }
      });
    }
  };
});

app.controller('mainCtrl', function($scope) {
  $scope.files = [];
  $scope.$on("fileSelected", function(event, args) {
    var item = args;
    $scope.files.push(item);
    var reader = new FileReader();

    reader.addEventListener("load", function() {
      $scope.$apply(function() {
        item.src = reader.result;
      });
    }, false);

    if (item.file) {
      reader.readAsDataURL(item.file);
    }
  });
});

当我点击提交按钮时,我想发送图像名称和相应的图像注释。我可以通过api cal发送数据用于php.in我的save()函数代码如下所示

$scope.save = function()
{
console.log($scope.files)console.log($scope.comments)
}

1 个答案:

答案 0 :(得分:0)

您需要使用$http服务将图像发送到服务器

$scope.save = function() {
    var fd = new FormData();
    fd.append('file', $scope.files);
    $http.post('uploadUrl', fd, {
            transformRequest: angular.identity,
            headers: {
                'Content-Type': undefined
            }
        })
        .then(function(response) {})
        .catch(function(response) {});
}