【问题标题】:AngularJS Upload and Post Multiple FilesAngularJS 上传和发布多个文件
【发布时间】:2016-01-04 22:15:37
【问题描述】:

所以最终,我正在做的是尝试使用 AngularJS 将多个文件上传和发布到 Django 后端。我可以发布单个文件,但似乎当 FileList 对象放入 $http.post 数据字段时,后端不再检测 POST 请求中的任何文件。

这是 html 的样子:

<form enctype="multipart/form-data" action="upload" method="POST">
  <div class="form-group">
    <span class="btn btn-info btn-file">
      <i class="glyphicon glyphicon-file"></i> Browse
      <input class="btn btn-lg btn-info" name="uploadedfile" type="file" accept=".eossa" onchange="angular.element(this).scope().filesUploaded(this.files)" multiple><br/>
    </span>
    <button type="button" class="btn btn-success" ng-click="uploadFiles()" ng-class="{'disabled':!model.files.length}">
      <i class="glyphicon glyphicon-download-alt"></i> Submit
    </button>
  </div>
  <pre ng-repeat="file in model.files" ng-show="file.name">{{file.name}}  ({{file.size/1000}} KB)  {{file.lastModifiedDate}} </pre>
</form>

下面是相关的 JavaScript:

$scope.filesUploaded = function(files) {
    if(files.length < 1) return;
    $scope.model.files = files;
    $scope.$apply();
};

$scope.uploadFiles = function(){
  var fd = new FormData();
  fd.append("file", $scope.model.files);
  Services.uploadFiles(fd)}

这是我的 uploadFiles 服务:

uploadFiles: function(form){
    return $http.post("/api/file-upload/", form, {withCredentials: true, headers: {'Content-Type': undefined }, transformRequest: angular.identity})
  },

在这种情况下,后端在request.FILES 中看不到任何内容;但是,如果不是附加$scope.model.files,而是附加$scope.model.file[0],我确实会在后端的请求中看到一个文件。在这种情况下,uploadFiles 函数如下所示:

$scope.uploadFiles = function(){
  var fd = new FormData();
  fd.append("file", $scope.model.files[0]);
  Services.uploadFiles(fd)
}

那么为什么不能将FileList 附加到Form 上呢?如何发布文件列表?

谢谢

【问题讨论】:

    标签: angularjs django forms file post


    【解决方案1】:

    首先创建一个如here指出的指令

    .directive('filesModel', function () {
        return {
            restrict: 'A',
            controller: function ($parse, $element, $attrs, $scope) {
                var exp = $parse($attrs.filesModel);
                $element.on('change', function () {
                    exp.assign($scope, this.files);
                    $scope.$apply();
                });
            }
        };
    });
    

    对于变换函数,check this out。 你可以使用这样的工厂:

        .factory('File', function () {
            return {
                // Define a function to transform form data
                transformRequest: function (data, headersGetter) {
                    var fd = data ? new FormData() : null;
                    if (data) {
                        angular.forEach(data, function (value, key) {
                            // Is it a file?
                            if (value instanceof FileList) {
                                if (value.length == 1) {
                                    fd.append(key, value[0]);
                                } else {
                                    angular.forEach(value, function (file, index) {
                                        fd.append(key + '_' + index, file);
                                    });
                                }
                            }
                            // Is it an object?
                            else if (typeof value === 'object') {
                                fd.append(key, JSON.stringify(value));
                            } else {
                                fd.append(key, value);
                            }
                        });
                    }
                    return fd;
                }
            };
        })
    

    那么对于服务:

    uploadFiles: function(form){
        return $http.post("/api/file-upload/", form, {withCredentials: true, headers: {'Content-Type': undefined }, transformRequest: File.transformRequest})
    }
    

    最后是html:

    <input type="file" files-model="<model>"/>
    

    或者这个用于多个文件

    <input type="file" files-model="<model>" multiple/>
    

    【讨论】:

    • 恰好我在当前项目中遇到了同样的问题;不客气
    【解决方案2】:

    我从来没有尝试过上面的方法,这可能是一个警察,但我想我会让你打开一些图书馆。

    我会检查这两个很棒的 AngularJS 文件上传 Repos。我个人使用这个。

    两者都支持轻松设置多个文件上传。

    ng-file-upload

    这是另一个。

    angular-file-upload

    既然已经有几十个人为您做出了贡献并为您完成了工作,为什么还要投入数小时的工作?

    【讨论】:

      猜你喜欢
      • 2020-04-24
      • 1970-01-01
      • 1970-01-01
      • 2013-06-06
      • 2015-08-27
      • 2017-06-04
      • 2017-12-04
      • 1970-01-01
      相关资源
      最近更新 更多