简体   繁体   English

文件上载在Ajax更新面板的页面中不起作用

[英]File Upload doesn't work in a page with Ajax Update Panel

I am using Ajax File Upload control in ASP.NET 4 using c#. 我正在使用c#在ASP.NET 4中使用Ajax文件上载控件。 The same page has an update panel too but the upload control is not inside the update panel. 同一页面上也有一个更新面板,但是上载控件不在更新面板内。 The upload control is outside of the update panel. 上传控件在更新面板之外。

The update panel has a captcha image and submit button which is described here too . 更新面板上有一个验证码图片和一个提交按钮, 这里也介绍 The submit button inside contains code for saving the file from upload control. 内部的“提交”按钮包含用于从上传控件保存文件的代码。

The problem is that when user has browsed the fife to be uploaded using upload control and then enters a wrong captcha value and submits, then a new captcha image is given asynchronously to the user for entry. 问题是,当用户使用上载控件浏览了要上传的文件名,然后输入了错误的验证码值并提交时,新的验证码图像会异步地提供给用户以供输入。 Now the upload control still shows the path in the upload bar for the file, but on the programming side it does not detects the file. 现在,上载控件仍在文件的上载栏中显示路径,但是在编程端它无法检测到文件。

The submit button code: 提交按钮代码:

if (AsyncFileUpload.HasFile)
{
  // upload logic and other stuff
}
else
{
  // lblShow.Text = "There is no file to be uploaded";
}

The above code for example executes the else part to say "There is no file to be uploaded". 上面的代码例如执行else部分说“没有文件要上传”。 The page still hasn't refreshed totally and the file upload control has the path of the file displayed. 该页面还没有完全刷新和文件上传控件中显示的文件的路径。 Kindly help me with this problem. 请帮助我解决这个问题。

If you are using Ajax update panel with file upload control then you have to add postback trigger into update panel triggers. 如果将Ajax更新面板与文件上传控制一起使用,则必须将回发触发器添加到更新面板触发器中。 Like: 喜欢:

<Triggers>
        <asp:PostBackTrigger ControlID="btnContactSubmit"/>                                                         
</Triggers>

If your code: 如果您的代码:

if (AsyncFileUpload.HasFile)
{
  // upload logic and other stuff
}
else
{
  // lblShow.Text = "There is no file to be uploaded";
}

is in the Page_Load event, it will still execute in the context of a partial post-back, eg the UpdatePanel refresh. Page_Load事件中,它将仍然在部分回发的上下文中执行,例如UpdatePanel刷新。 If a full form submit has not been performed from the browser (you mentioned your File Upload is outside of the UpdatePanel ) then the page will not detect the file upload. 如果尚未从浏览器执行完整表单提交(您提到文件上传不在UpdatePanel之外),则页面将不会检测到文件上传。

What I am confused about is why you have called it AsyncFileUpload when it is outside the UpdatePanel ? 我感到困惑的是,为什么在UpdatePanel之外将其称为AsyncFileUpload

EDIT: 编辑:

Based on your answer, I don't think your Captcha implementation is workable with async file upload as you have it now. 根据您的回答,我认为您的Captcha实现不适用于异步文件上传,因为您现在拥有它。

The UpdatePanel does an async POST to evaluate captcha result, but you will not POST the file contents yet because its not inside the UpdatePanel. UpdatePanel会执行异步POST来评估验证码结果,但是您还不会发布文件内容,因为它不在UpdatePanel中。 Then your server-side code evaluates the captcha result and will either return html or a redirect in the async-response back to the browser... somewhere you need to eventually submit the form to get the file. 然后,您的服务器端代码将对验证码结果进行评估,并将返回html或异步响应中的重定向返回浏览器...您最终需要提交表单以获取文件的位置。

Unless you're prepared to write code to send some javascript back to your page in the async-response to trigger a full form submit, AND re-evaluate CAPTCHA again on the form submit, you're probably better off taking out the UpdatePanel , in my opinion. 除非您准备编写代码以在异步响应中将一些javascript发送回您的页面以触发完整的表单提交,并且再次对表单提交重新评估验证码,否则最好删除UpdatePanel ,在我看来。

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

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