1

i want to send multiple image data with textbox value to server side(PHP).i have done multiple image upload but i am not able to send my data to server side when submitting form.my view code is below

<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>

in my controller

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);
    }
  });
});

when i click submit button i want to send image name with corresponding comments for the image.how could i send the data through api cal for php.in my save() function code looks below

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

1 Answer 1

0

you need to use the $http service to send images to the server

$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) {});
}
Sign up to request clarification or add additional context in comments.

5 Comments

i want to send text box value with image
change ng-model variable to file object property. like <input type="text" ng-model="step .comments"> This will bind the comment to files array objects
if i put console.log($scope.files) after this fd.append('file', $scope.files); not able to get any image name
can you please make plunkr for this
but here i am not able to preview my image

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.