php – AngularJS使用FormData API上传多个文件

我需要使用Laravel 5.1作为后端,在Angular应用程序中将图像和视频文件上传到服务器.所有Ajax请求首先需要转到Laravel控制器,我们在那里有代码,用于在文件到达时如何处理文件.我们之前已经完成了普通的HTML表单来向控制器提交文件上传,但在这种情况下我们需要避免页面刷新表单,所以我在Ajax中通过Angular尝试这个.

我需要使用之前通过HTML表单发送到控制器的Ajax发送到Laravel控制器的哪些信息?

这是Laravel控制器中的代码,一旦到达那里就处理文件信息.这就是我需要弄清楚如何发送,所以我希望可以重用这段代码

    $promotion = Promotion::find($id);
    if (Input::hasFile('img_path')){
        $path             = public_path().'/images/promotion/'.$id.'/';
        $file_path        = $path.'promotion.png';
        $delete           = File::delete($file_path);
        $file             = Input::file('img_path');
        $uploadSuccess    = $file->move($path, 'promotion.png');
        $promotion->img_path = '/images/promotion/'.$id.'/promotion.png';
    }
    if (Input::hasFile('video_path')){
        $path             = public_path().'/video/promotion/'.$id.'/';
        $file_path        = $path.'promotion.mp4';
        $delete           = File::delete($file_path);
        $file             = Input::file('video_path');
        $uploadSuccess    = $file->move($path, 'promotion.mp4');
        $promotion->video_path = '/video/promotion/'.$id.'/promotion.mp4';
    }

正如您在上面所看到的,我们将文件名为promotion.png的文件转换为PNG,因此很容易获取,我们只接受.mp4视频格式.因此,我们不需要担心检查文件是否存在,是否可以覆盖它.这就是为什么你可以在代码中看到我们在保存之前删除名称的任何现有文件.

HTML只是一个输入类型为“文件

<input type="file" id="img_path" name="img_path" class="promo-img-path" accept="image/*">

我们现在正在使用Angular,因此我不能再通过HTML表单发送上述内容了.这就是我需要弄清楚如何做的事情.

我们是两位开发人员,只是做得最好,所以我相信有更好的方法可以做到这一点.然而,在我重构整个事情之前,我希望我可以使用Angular(或jQuery作为最后的手段)来向控制器发送Laravel所需的任何文件数据,以使上述代码工作.答案可能就像“将PUT发送到上面那个控制器中的方法一样简单,但不是普通的JSON有效负载,而是使用这种格式的文件信息,你可以用……收集信息”.

我也很感激任何关于我将来能够做到这一点的更好方法提示.

解决方法:

如何使用$http服务发布FormData

使用FormData API POST文件和数据时,务必将Content-Type header设置为undefined.

var fd = new FormData()
for (var i in $scope.files) {
    fd.append("filetoUpload", $scope.files[i]);
}
var config = {headers: {'Content-Type': undefined}};

var httpPromise = $http.post(url, fd, config);

认情况下,AngularJS框架使用内容类型application / json.通过设置Content-Type:undefined,AngularJS框架省略了允许XHR API设置内容类型的内容类型标头.发送FormData object时,XHR API将内容类型设置为具有适当边界和base64编码的multipart / form-data.

有关更多信息,请参阅MDN Web API Reference – XHR Send method

How did you get the file information into $scope.files?

如何启用< input type =“file”>使用ng-model

该指令还启用< input type =“file”>自动使用ng-change和ng-form指令.

angular.module("app",[]);

angular.module("app").directive("selectFilesNg", function() {
  return {
    require: "ngModel",
    link: function postLink(scope,elem,attrs,ngModel) {
      elem.on("change", function(e) {
        var files = elem[0].files;
        ngModel.$setViewValue(files);
      })
    }
  }
});
<script src="//unpkg.com/angular/angular.js"></script>
  <body ng-app="app">
    <h1>AngularJS Input `type=file` Demo</h1>
    
    <input type="file" select-files-ng ng-model="fileArray" multiple>

    <code><table ng-show="fileArray.length">
    <tr><td>Name</td><td>Date</td><td>Size</td><td>Type</td><tr>
    <tr ng-repeat="file in fileArray">
      <td>{{file.name}}</td>
      <td>{{file.lastModified | date  : 'MMMdd,yyyy'}}</td>
      <td>{{file.size}}</td>
      <td>{{file.type}}</td>
    </tr>
    </table></code>
    
  </body>

推荐:直接发布二进制文件

发布具有多部分/表单数据的二进制文件效率低,因为base64 encoding增加了额外33%的开销.如果服务器API接受带有二进制数据的POST,请直接发布文件.

How to POST binary files with AngularJS (with DEMO)

相关文章

统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
前言 之前做了微信登录,所以总结一下微信授权登录并获取用户...
FastAdmin是我第一个接触的后台管理系统框架。FastAdmin是一...
之前公司需要一个内部的通讯软件,就叫我做一个。通讯软件嘛...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...