如何在AngularJS中上传多个文件

时间:2017-01-20 17:06:54

标签: javascript angularjs ng-file-upload

我使用ngFileUpload上传文件,但它只能上传1个文件。

我想上传图片文件和PDF文件。如何上传多个文件?

请帮帮我。

(function () {
    'use strict';
    var app = angular.module('pageApp', ['ngFileUpload']);

    app.controller('MyCtrl', ['$scope', 'Upload', '$timeout', function ($scope, Upload, $timeout) {
        $scope.uploadPic = function (file) {
            file.upload = Upload.upload({

                url: 'save_form.php',
                data: {
                    file: file,
                    username: $scope.username
                },
            });

            file.upload.then(function (response) {
                $timeout(function () {
                    file.result = response.data;
                });
            }
        };

    }]);

})();
  <form name="myForm">    
      Profile Picture: <input type="file" ngf-select="" ng-model="picFile" name="file" ngf-accept="'image/*'">
      <br>
      Resume(.pdf): <input type="file" ngf-select="" ng-model="resumeFile" name="fileResume">
      <br>

      <button ng-disabled="!myForm.$valid" ng-click="uploadPic(picFile)">Submit</button>
  </form>

1 个答案:

答案 0 :(得分:0)

如果要同时上传多个文件,可以组合这两个输入字段并将多个属性添加到输入标记。确保您的后端可以接受多个文件/支持以下功能。

<form name="myForm">    
      Picture & Resume: <input type="file" ngf-select="" 
               ng-model="picFile" name="file"  ngf-pattern="'image/*,application/pdf'" multiple>
      <br>

      <button ng-disabled="!myForm.$valid" ng-click="uploadPic(picFile)">Submit</button>
  </form>