从角度控制器发送带有文本框的多个图像数据

2023-11-30

我想将带有文本框值的多个图像数据发送到服务器端(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);
    }
  });
});

当我单击提交按钮时,我想发送图像名称以及图像的相应注释。我如何通过 php 的 api cal 发送数据。在我的 save() 函数代码中如下所示

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

你需要使用$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) {});
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

从角度控制器发送带有文本框的多个图像数据 的相关文章

随机推荐