gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 20:25:45 25 4
gpt4 key购买 nike

我想将多个文件上传到 Rails 服务器,并希望为每个文件显示单独的进度条。我正在使用 ng-file-upload 上传文件。文件正在上传,但它只显示最后一个文件的进度,而不显示其他文件的进度。我附上了我的代码。请帮忙。

Angular Controller 代码:

$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代码:

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

最佳答案

我无法测试以下内容,但我想我发现了问题所在。

在 JavaScript 中,变量的作用域是函数。因此,在您的 for 循环中,您更改 var file = files[i]; 行中 file 的值。最后,for 循环结束后,file 的值就是最后一个文件。

在某些时候 .progress 事件由 ng-file-upload 触发,以通知您有关进度(对于其中一个文件)。您更新了状态,但是,由于 file 具有最后一个文件的值,而不是您期望的值,因此正在更新最后一个文件的状态。

这就是为什么只更新最后一个文件的原因。要解决这个问题,您需要为每个 progress 事件访问正确的 file。为此,您可以使用匿名函数保留 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])
}

在您的代码中,可能存在与变量范围和 JavaScript 事件循环相关的其他问题。有关更多信息,请查看 this .

关于ruby-on-rails - 带有多个文件上传和进度条的 ng-file-upload,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28848232/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com