Search code examples
c#asp.netupdatepanelcaptchaasyncfileupload

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#. 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". 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.


Solution

  • 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, e.g. the UpdatePanel refresh. 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.

    What I am confused about is why you have called it AsyncFileUpload when it is outside the UpdatePanel?

    EDIT:

    Based on your answer, I don't think your Captcha implementation is workable with async file upload as you have it now.

    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. 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.

    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.