繁体   English   中英

如何等待多个jquery文件上传?

[英]How do I wait for multiple jquery file uploads?

我正在尝试使用jquery文件上传功能上传多个文件后刷新页面:

由于某种原因,在每个文件上载完成后都会调用我的测试消息,而我试图在所有文件上载后仅调用一次。 这可能吗?

HTML:

<form id="fileupload" action="server/php/" method="POST" enctype="multipart/form-data">
    <!-- The fileupload-buttonbar contains buttons to add/delete files and start/cancel the upload -->
    <div class="row fileupload-buttonbar">
        <div class="span7">
            <!-- The fileinput-button span is used to style the file input field as button -->
            <span class="btn btn-success fileinput-button">
                <i class="icon-plus icon-white"></i>
                <span>Add files...</span>
                <input type="file" name="files[]" multiple>
            </span>
            <button type="submit" class="btn btn-primary start">
                <i class="icon-upload icon-white"></i>
                <span>Start upload</span>
            </button>
        </div>
    </div>
    <!-- The loading indicator is shown during file processing -->
    <div class="fileupload-loading"></div>
    <br>
    <!-- The table listing the files available for upload/download -->
    <table role="presentation" class="table table-striped"><tbody class="files" data-toggle="modal-gallery" data-target="#modal-gallery"></tbody></table>
</form>

jQuery查询

$('#fileupload').bind('fileuploadcompleted', function (e, data) {
    e.preventDefault();

    // I would expect to see this after all files ahve uplaoded but I see it for each file upload ?
    alert("Completed");

    //location.reload();
    ...
})

监听fileuploadadd事件,每次它发生时,它都会增加一个变量,以便您知道有多少个文件要上传。 然后,在您的fileuploaddone事件处理程序中,递增另一个变量,以便您知道有多少个变量已完成。 当两者相等时,所有上传均已完成,请重新加载(或可能需要执行的其他任何操作)。 似乎应该有一个更好的解决方案,但是这个解决方案应该可以工作。

仅在所有上传完成后才触发stop事件。

$('#fileupload').bind('fileuploadstop', function (e, data) {
    location.reload();
})

暂无
暂无

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

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