简体   繁体   English

使用Angular.js上传后如何收集数组中的多个文件

[英]How to collect multiple files in array after upload using Angular.js

I need one help.I have multiple file upload functionality in my app.Here i need to collect all files in an array so that i can upload those and also display them after selecting.I am explaining my code below. 我需要一个帮助。我的应用程序中有多个文件上传功能。这里我需要将所有文件收集到一个数组中,以便我可以上传这些文件并在选择后显示它们。我在下面解释我的代码。

<div class="input-group bmargindiv1 col-md-12" ng-repeat="mul in mulImage">
<span class="input-group-addon ndrftextwidth text-right" style="width:180px">Image{{$index+1}}:</span>
 <div>
<div ng-class="{'myError': billdata.uploadme_{{$index}}.$touched && billdata.uploadme_{{$index}}.$invalid }">
<input type="file" class="filestyle form-control" data-size="lg" name="upload_{{$index}}" id="bannerimage_{{$index}}"  ng-model="mul.image" ngf-pattern="'image/*'" accept="image/*" ngf-max-size="2MB" ngf-min-height="400" ngf-resize="{width: 400, height:400}"  ngf-select="onFileSelect('upload_{{$index}}',mul.image);">
<div style="clear:both;"></div>
</div>
</div>
<span class="input-group-btn" ng-show="showImage{{$index}}">
<img ng-src="{{attachmultipleimage_$index}}" name="pro" border="0" style="width:32px; height:32px; border:#808080 1px solid;">
<input type="button" class="btn btn-success" name="plus" id="plus" value="+" ng-click="addNewImageRow(mulImage);" ng-show="$last"> <input type="button" class="btn btn-danger" name="minus" id="minus" value="-"  ng-show="mulImage.length>1" ng-click="deleteNewImageRow(mulImage,$index);">
 </span>
 <div class="help-block" ng-messages="billdata.uploadme_{{$index}}.$error" ng-if="billdata.uploadme_{{$index}}.$touched">
 <p ng-message="maxSize" style="color:#F00;">File is too large.Max size is 2 mb.</p>
 <p ng-message="minHeight" style="color:#F00;">Minimum height should be 400px</p>
</div>
 </div>
 </div>

Here when i am clicking on plus button one new image row is creating and similarly when i will click on minus button one image uploading row will eliminate.My controller side code is given below. 在这里,当我单击plus按钮时,将创建一个新的图像行,类似地,当我单击minus按钮时,将删除一个图像上传行。下面给出了我的控制器端代码。

$scope.mulImage=[];
   $scope.mulImage.push({'image':null});
   $scope.addNewImageRow=function(mulImage){
       mulImage.push({'image':null});
   }
   $scope.deleteNewImageRow=function(mulImage,index){
       mulImage.splice(index,1);
   }
   $scope.onFileSelect = function(name,$files,index) {
        var files =$files;
        for (var i = 0; i < files.length; i++) {
             var file = files[i];
             var reader = new FileReader();
             reader.onload = $scope.imageIsLoaded(index); 
             reader.readAsDataURL(file);
        }
   }

I have already done something.Here i need to display the selected image ( img ng-src="{{attachmultipleimage_$index}}" ) in each new created row and collect all the selected files to upload.Please help me. 我已经做了一些事情。在这里,我需要在每个新创建的行中显示选定的图像( img ng-src="{{attachmultipleimage_$index}}" ),并收集所有选定的文件进行上传。请帮助我。

It looks like angular-file-upload is better suited for you because it has this functionality built in. It's file uploader instance has an array queue which contains all the selected files and also has lots of helper methods. 看起来angular-file-upload更适合您,因为它内置了此功能。它的文件上传器实例具有一个数组queue ,该queue包含所有选定的文件,并且还具有许多辅助方法。

this example does what you want to do, select multiple image files and preview them. 本示例执行您想要的操作,选择多个图像文件并预览它们。

Major code would be: 主要代码为:

angular.module('app', ['angularFileUpload'])
  .controller('AppController', ['$scope', 'FileUploader',
      function($scope, FileUploader) {
        // create an instance
        $scope.uploader = new FileUploader({
          url: 'upload.php'
        });
      });

Then you can use it like: 然后您可以像这样使用它:

<input type="file" nv-file-select="" uploader="uploader" multiple />
<br/>
<div ng-repeat="item in uploader.queue">
  <strong>{{ item.file.name }}</strong>
  <div ng-show="uploader.isHTML5" ng-thumb="{ file: item._file, height: 100 }"></div>
</div>

ng-thumb is a directive written by it's author for image preview example. ng-thumb是它的作者编写的用于图像预览示例的指令。

In short, just grab this image-preview and modify it if necessary 简而言之,只需获取此图像预览并在必要时对其进行修改

The following will allow to add files to the queue and remove them. 下面将允许将文件添加到队列中并删除它们。 The key is to use ngf-keep="true" . 关键是使用ngf-keep="true"

<button ngf-select ng-model="allFiles" ngf-keep="true">Add File</button>

<div ng-repeat="f in allFiles">
    <img ngf-thumbnail="f">
    <button ng-click="removeFile($index)">delete</button>
</div>

<button ng-click="upload()">upload all</button>


$scope.removeFile = function(index) {
    $scope.allFiles.splice($index, 1);
    $scope.allFiles = $scope.allFiles.slice(0);
}

$scope.upload = function() {
    Upload.upload({..., data: {file: allFiles}});
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM