0

I am using angularjs. I have created a file upload function using angularjs with laravel. The file upload with FormData is working fine. But when I try to send file through PUT method there is no response from the server side.

I have gone through the following answers. Uploading a file with jquery and sending as a PUT and How to upload a file using an HTTP "PUT" using JQuery? but I cannot able to find solution.

Here is my code.

<input type="file" ng-file-model = "formData.files" multiple>

The directive for my code

app.directive('ngFileModel', ['$parse', function ($parse) {
return {
    restrict: 'A',
    link: function (scope, element, attrs) {
        var model = $parse(attrs.ngFileModel);
        var isMultiple = attrs.multiple;
        var modelSetter = model.assign;
        element.bind('change', function () {
            var values = [];
            angular.forEach(element[0].files, function (item) {
                var value = item;
                values.push(value);
            });
            scope.$apply(function () {
                if (isMultiple) {
                    modelSetter(scope, values);
                } else {
                    modelSetter(scope, values[0]);
                }
            });
        });
      }
    };
}]);

Here is my function which converts my form data into FormData

constructFormData : function( data ) {
  var fd = new FormData();
  for (var key in data) {
    var value = data[key];
    angular.forEach(data, function (file, i) {
      fd.append('files['+i+']', file);
    });
  }
  return fd;
}, 

Here is my Controller

var formData = GeneralService.constructFormData($scope.formData);
FileService.update( formData )
  .success(function( data ){
      if(data.status == 403) {
        $location.path('/403');
      }
      if(data.success) {
          console.log(data);
      } else {
        ValidationService.showValidationErrors(data.errors);
      }
  });

Here is my service

update : function(formData) {
  return $http({
        method: 'PUT',
        url: $rootScope.baseurl +'/files',
        data: formData,
        dataType: 'json',
        headers: {'Content-Type': undefined}
    });
},

The Server side (laravel)

routes.php

Route::put('/files', ['uses' => 'FilesController@update']);

FilesController

public function update(Request $request) {
  $data = $request->all();
  print_r($data);
}

the above print_r function displays nothing.

I am get the posted data using print_r($request->all());, It gives empty data. I don't know where I make mistake. If I am asking this question by wrong please apologize.

15
  • where is constructFormData called? Commented May 9, 2016 at 13:09
  • 1
    What is your backend controller expecting? Do you have a web method that is expecting PUT or POST? Commented May 9, 2016 at 13:16
  • I need to send file using PUT Commented May 9, 2016 at 13:17
  • 1
    Does your server have an endpoint expecting PUT? What does that look like? Commented May 9, 2016 at 13:23
  • 1
    We need to see the backend,. Commented May 9, 2016 at 13:23

1 Answer 1

1

I have the same issue and finally i found this code

var formData = new FormData();
angular.forEach($scope.data, function (value, key) {
    formData.set(key, value);
});

$http.post(uploadUrl, formData, {
    transformRequest: angular.identity,
    headers         : {'Content-Type': undefined}
});
Sign up to request clarification or add additional context in comments.

3 Comments

This is using post not put.
@Dan you can use put instead of post function, the $http has put method also
This question is about using PUT, which you cannot do, it's a PHP core issue: stackoverflow.com/a/55725521/1072492

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.