Fileupload多次上传文件 [英] Fileupload uploads files multiple times

查看:506
本文介绍了Fileupload多次上传文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在asp.net网站上使用jquery blueimp fileupload遇到了一个奇怪的问题.经过一番尝试后的错误,我让它运行起来没有太大问题,并且它或多或少地正常工作.但是,有些奇怪的行为我无法解决.作为所有jquery插件的规范,页面加载后必须对其进行初始化.但是,我不能使用常规的$(document).ready,因为必须在其中使用的Web窗体包含多个updatepanels,因此我需要使用pageLoad函数以使插件在任何隐藏的回发之后保持初始化状态. updatepanel可能会触发.现在,一旦我上传,就会出现奇怪的行为.如果我在网络表单中执行任何其他操作(导致从更新面板执行操作),则我尝试上传的下一个文件将上传两次.如果我执行其他操作,则我上传的下一个文件将被上传3次,等等.

尝试销毁每个pageLoad上的插件似乎没有什么区别.并在$(document).ready中初始化它无效,因为这仅在第一次加载表单时发生.有任何想法吗?.谢谢

解决方案

是的,我解决了,对不起,我忘记了我在这里提出的要求,所以我没有回来发表答案,所以遇到类似问题的人可能在这里结束可以阅读.

诀窍是不是在每个pageLoad事件上而是在请求开始时才在插件上调用destroy.和往常一样,asp.net updatepanels和jquery在一起工作时非常棘手.

做类似

Sys.WebForms.PageRequestManager.getInstance().add_beginRequest(pageBeginRequest);
function pageBeginRequest() {
    $('#fileupload').fileupload();
    $('#fileupload').fileupload('destroy');   
}

将确保该插件已销毁,因此它在pageLoad上再次保持新鲜,从而避免了多次绑定事件.这就是为什么文件被上传的次数与部分回发一样多的原因.

I'm having a strange problem using the jquery blueimp fileupload on an asp.net website. After some trial an error i have it up an running without much problem, and it's working more or less properly. However, there's some strange behaviour i can't work out. As the norm with any jquery plugins, it has to be initialized once the page loads. However, i can't use a regular $(document).ready due to the fact the webform it has to be used in contains multiple updatepanels, hence i need to use a pageLoad function in order to keep the plugin initialized after any hidden postback the updatepanel may fire. Now, the strange behaviour comes once i do an upload. If i do any other action in the webform (causing an action from the update panel) the next file i try to upload is uploaded twice. If i do yet another action, the next file i upload is uploaded 3 times, etc.

Trying to destroy the plugin on every pageLoad seems to make no difference. And initializing it inside a $(document).ready makes no effect, since this only happens on the very first form load. Any ideas?. Thanks

解决方案

Yeah, i solved it, sorry, i forgot i had asked it in here so i didnt come back to post an answer, so anyone suffering a similar problem who may end here could read it.

The trick is calling destroy on the plugin not on every pageLoad event, but right at the start of the request. As usual, asp.net updatepanels and jquery are quite tricky when they gotta work together.

Doing something like

Sys.WebForms.PageRequestManager.getInstance().add_beginRequest(pageBeginRequest);
function pageBeginRequest() {
    $('#fileupload').fileupload();
    $('#fileupload').fileupload('destroy');   
}

will make sure the plugin is destroyed, so it's fresh again on the pageLoad, avoiding the events being binded multiple times. That's why the files get uploaded as many times as partial postbacks have taken place.

这篇关于Fileupload多次上传文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆