使用PUT方法Angularjs发送文件 [英] Send file using PUT method Angularjs

查看:105
本文介绍了使用PUT方法Angularjs发送文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用angularjs.我已经使用laravel和angularjs创建了一个文件上传功能.使用FormData上传的文件运行正常.但是当我尝试通过PUT方法发送文件时,服务器端没有响应.

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.

我已经回答了以下问题. 使用jquery上载文件并作为PUT发送如何使用HTTP"PUT"使用JQuery吗?,但是我找不到解决方案.

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.

这是我的代码.

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

我的代码的指令

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

这是我的函数,它将我的表单数据转换为FormData

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

这是我的控制器

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

这是我的服务

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

服务器端(laravel)

The Server side (laravel)

routes.php

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

FilesController

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

上面的print_r函数什么都不显示.

the above print_r function displays nothing.

我使用print_r($request->all());获取发布的数据,它提供了空数据.我不知道我在哪里犯错.如果我错误地问了这个问题,请道歉.

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.

推荐答案

我遇到了同样的问题,最后我找到了这段代码

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

这篇关于使用PUT方法Angularjs发送文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆