简体   繁体   English

带有多个文件上传和进度条的ng-file-upload

[英]ng-file-upload with multiple file upload and progress bar

I want to upload multiple files to rails server and want to show separate progress bar for each files. 我想将多个文件上传到rails服务器,并希望为每个文件显示单独的进度条。 I am using ng-file-upload to upload files. 我正在使用ng-file-upload来上传文件。 files are getting uploaded but it showing progress for only last file and not for others..I am attaching my code. 文件正在上传,但它只显示最后一个文件的进度,而不显示其他文件。我正在附加我的代码。 please help. 请帮忙。

Angular controller code: 角度控制器代码:

$scope.upload_file = function() {

    var files = $scope.files;
    var uploadUrl = base_url+"/upload_image";
    var job_id = $scope.directive.id;
    if(current_user.role == "third_party_vendor" || current_user.role == "employee")
    {
        var source = current_user.user_login+"-"+current_user.role;   
    }
    else
    {
        var source = $scope.dataObj.source["user_login"]+"-"+$scope.dataObj.source["role"];
    }

    if(job_id === "" || job_id === undefined || files === undefined || files === ""){
        error_notification("Please select a job and a file.");
        return;
    }
    hideLoader();
    $("#upload_resume_queue").modal('show');

    var formData = new Array();
    formData['job_id'] = job_id;
    formData['context'] = "inside_page";
    formData['source'] = source;

    for (var i = 0; i < files.length; i++) {
      var file = files[i];
      console.log(file.name);
      $scope.upload = $upload.upload({
        url: uploadUrl, 
        data:{myObj: formData},
        file: file, 
      }).progress(function(evt) {
        //console.log('percent: ' +parseInt(100.0 * evt.loaded / evt.total));


                file.progress = Math.round(evt.loaded * 100 / evt.total)


      }).success(function(responseText) {
        hideLoader();
        try{
            var response = responseText;
        }catch(e){
            error_notification("Invalid Excel file Imported.");
            return;
        }
        if(response.status==='wrong_content_type')
            error_notification("Please upload a valid file format.",0);
        if(response.status==='job_application_present'){
            $scope.duplicate = true;
            $scope.jobID = job_id;
            $scope.user_id = response.user_id;
            $scope.application_id = response.application_id;
            //showModal('#duplicate_application_modal');
            error_notification("Job Application already present for this user and job.",0);
        }
        if(response.status==='invalid_email')
            error_notification("The email in the resume is an invalid one.",0);
        if(response.status==='success')
            success_notification("The uploaded resume has been parsed.",0);
      });
    }
  };

Html code: Html代码:

<input type="file" class="required file_browse" ng-file-select="" ng-model="files" multiple />

I am not able test the following but, I think I found what is wrong. 我无法测试以下但是,我认为我发现了什么是错的。

In JavaScript variables are scoped to functions. 在JavaScript中,变量的作用域是函数。 So, in your for loop you change value of file in var file = files[i]; 因此,在for循环中,您可以在var file = files[i];更改filevar file = files[i]; line. 线。 At the end, after for loop finished the value of file is the last file. 最后,在for循环完成后, file的值是最后一个文件。

At some point .progress event is fired by ng-file-upload to notify you about progress (for one of the files). 在某些时候, ng-file-upload会触发.progress事件,以通知您进度(对于其中一个文件)。 And you update the status, but, since file has the value of the last file, not the one you expected to be, the last file's status is being updated. 并且您更新状态,但是,由于file具有最后一个文件的值,而不是您期望的那个,因此正在更新最后一个文件的状态。

That's why only the last file updated. 这就是为什么只更新了最后一个文件。 To solve, you need to access the correct file for each progress event. 要解决此问题,您需要为每个progress事件访问正确的file To do this, you can keep file variable using an anonymous function. 为此,您可以使用匿名函数保留file变量。

for (var i = 0; i < files.length; i++) {
  (function(file) {
    console.log(file.name);
    $scope.upload = $upload.upload({
      url: uploadUrl, 
      data:{myObj: formData},
      file: file, 
    }).progress(function(evt) {
      //console.log('percent: ' +parseInt(100.0 * evt.loaded / evt.total));
      file.progress = Math.round(evt.loaded * 100 / evt.total)
    }).success(function(responseText) {
      // the other stuff
    });
  })(files[i])
}

In your code there may be other problems related to variable scopes and javascript event loop. 在您的代码中,可能存在与变量作用域和javascript事件循环相关的其他问题。 For more information please take a look at this . 有关更多信息,请查看此内容

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

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