Valums Ajax Uploader (Mutli) - 检测所有文件何时上传

发布于 2024-10-11 01:24:38 字数 931 浏览 8 评论 0原文

我正在使用 Valums Ajax Uploader 上传一批文件。我们最近将代码从单一上传类型更改为多重上传类型。这给我们的代码带来了问题。

如您所见,当 onComplete 事件触发时,我们重新加载页面以显示新上传的图像。但是,onComplete 事件似乎是在每个文件完成后触发,而不是在整个批次完成后触发。现在,这会导致问题,因为当第一个文件完成时,会触发页面重新加载尝试,并且上传者会弹出警报“如果您离开此页面,那么剩余的上传将会崩溃”或类似的内容。

我注意到 onComplete 事件发送回已完成文件的基于 0 的 ID,但我不确定如何使用它来确定批处理何时完成。

我想我的问题是 A) 当所有文件完成时是否会触发不同的事件或 B) 如何确定用户选择了多少个文件,以便在 onComplete 事件中进行跟踪已完成多少个文件?

    var uploader = new qq.FileUploader({
        multiple: true,
        element: document.getElementById('file-uploader'),
        action: '/projectPhotoUpload.php',
        allowedExtensions: ['jpg', 'png', 'gif'],
        debug: true,
        params: {id: i},
        onComplete: function(id, fileName, responseJSON){
            window.location = 'projects.php?all=true&tab=1&sel=' + currProject;                                 
        }   
    })  

I am using the Valums Ajax Uploader to upload a batch of files. We recently changed the code from a single-upload a multi-upload type. This has raised a problem with our code.

As you can see, when the onComplete event fire, we reload the page to show newly uploaded images. However, the onComplete event seems to be firing after EACH file completes, and not after the entire batch does. This now causes a problem because when the first file finishes, the page reload attempt is fired and the uploader pops up an alert "If you leave this page, all heck will break loose on your remaining uploads" - or something to that effect.

I notice the onComplete event sends back a 0-based ID of the completed file, but I am not sure exactly how to use this to determine when the batch is done.

I guess my question is A) Is there a different event that triggers when all files are complete or B) How do I determine how many files the user has selected, in order to keep track in the onComplete event how many files have completed?

    var uploader = new qq.FileUploader({
        multiple: true,
        element: document.getElementById('file-uploader'),
        action: '/projectPhotoUpload.php',
        allowedExtensions: ['jpg', 'png', 'gif'],
        debug: true,
        params: {id: i},
        onComplete: function(id, fileName, responseJSON){
            window.location = 'projects.php?all=true&tab=1&sel=' + currProject;                                 
        }   
    })  

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

£噩梦荏苒 2024-10-18 01:24:38

您可以添加一个计数器,该计数器在上传开始时递增,在完成时递减。仅当计数器为 0 时才重定向完成。

var running = 0;  
var uploader = new qq.FileUploader({
    multiple: true,
    element: document.getElementById('file-uploader'),
    action: '/projectPhotoUpload.php',
    allowedExtensions: ['jpg', 'png', 'gif'],
    debug: true,
    params: {id: i},
    onSubmit: function(id, fileName){
        running++;                                 
    },
    onComplete: function(id, fileName, responseJSON){
        running--;
        if(running==0){
          window.location = 'projects.php?all=true&tab=1&sel=' + currProject;                                 
        }
    }   
}) 

You could add a counter that increments when an upload is started and decrements when complete. Only redirect on complete when the counter is 0.

var running = 0;  
var uploader = new qq.FileUploader({
    multiple: true,
    element: document.getElementById('file-uploader'),
    action: '/projectPhotoUpload.php',
    allowedExtensions: ['jpg', 'png', 'gif'],
    debug: true,
    params: {id: i},
    onSubmit: function(id, fileName){
        running++;                                 
    },
    onComplete: function(id, fileName, responseJSON){
        running--;
        if(running==0){
          window.location = 'projects.php?all=true&tab=1&sel=' + currProject;                                 
        }
    }   
}) 
夏の忆 2024-10-18 01:24:38

很好的答案,出于兴趣,如果您想检查responseJSON以查看文件是否已上传,它在服务器脚本上设置

return array('success'=>true, 'filename'=>$filename . '.' . $ext);

然后您可以在JS中像

var success = responseJSON["success"]
var filename = responseJSON["filename"]

这样您可以创建文件列表并检查名称完成时,或仅递减

if (success == true)

如果上传失败,那么您可能不希望它递减,例如

Good answer, out of interest if you wanted to check the responseJSON to see if the files had uploaded, its set on the server script

return array('success'=>true, 'filename'=>$filename . '.' . $ext);

Then you can pick it up in the JS like

var success = responseJSON["success"]
var filename = responseJSON["filename"]

This way you could make a list of files and check the names on completion, or only decrement

if (success == true)

If the upload had failed, then you might not want it to decrement for example

我一向站在原地 2024-10-18 01:24:38

您需要添加取消事件,以防用户取消文件上传:

onCancel: function(id, fileName){running--;}

我正在讨论在此处实施的正确方法。我还希望在文件上传后进行页面重定向,但如果用户打算上传更多文件怎么办?文件可能分布在不同的目录中。

似乎最好的方法是有一个“我完成”按钮,一旦用户完成上传,该按钮就会重定向。

You'll need to add a cancel event in case the user cancels out of a file upload:

onCancel: function(id, fileName){running--;}

I'm debating the right approach to take here on my implementation. I also would like to have the page redirect once the files are uploaded but what if the user intends to upload more files; the files may be spread out in separate directories.

Seems like the best approach is to have an "I'm Done" button that will redirect once the user is finished uploading.

Saygoodbye 2024-10-18 01:24:38

一种更简单的方法是使用内部属性_filesInProgress来检查是否所有上传的文件都已被处理。
它会给出:

var uploader = new qq.FileUploader({
    multiple: true,
    element: document.getElementById('file-uploader'),
    action: '/projectPhotoUpload.php',
    allowedExtensions: ['jpg', 'png', 'gif'],
    debug: true,
    params: {id: i},
    onComplete: function(id, fileName, responseJSON){
        if(uploader._filesInProgress == 0){
            window.location = 'projects.php?all=true&tab=1&sel=' + currProject;
        }                                 
    }   
});

A simpler way would be to use the inner property _filesInProgress to check if all uploaded files have been treated.
It would give:

var uploader = new qq.FileUploader({
    multiple: true,
    element: document.getElementById('file-uploader'),
    action: '/projectPhotoUpload.php',
    allowedExtensions: ['jpg', 'png', 'gif'],
    debug: true,
    params: {id: i},
    onComplete: function(id, fileName, responseJSON){
        if(uploader._filesInProgress == 0){
            window.location = 'projects.php?all=true&tab=1&sel=' + currProject;
        }                                 
    }   
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文