Praveen Srinivasan
Praveen Srinivasan

Reputation: 1620

Send file using PUT method Angularjs

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.

Upvotes: 0

Views: 1832

Answers (1)

Farhad Mohammadi
Farhad Mohammadi

Reputation: 41

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

Upvotes: 1

Related Questions