Home

Thursday, February 24, 2011

File Upload not Working on First PostBack after loading Async through ASP.NET UpdatePanel

I was setting up a ASP.NET Wizard control.  The 3rd or 4th step had a file upload control on it, with a runat="server".  The entire wizard control I wrapped in an ASP.NET UpdatePanel so that the wizard was all AJAXified.  I had already fixed the "next" button for this wizard step so that it did a full postback to the server, not an asynchronous one - since I knew that was required to get a file upload to the server.  However, the file was STILL not being posted back, atleast not on the first post.  Subsequent posts would work fine.  On a whim, I looked at the Firebug console to inspect the details of the request while it was in progress (Set a breakpoint in your code somewhere, then look at the Firebug console -- you'll see a "POST" entry with info about what was posted, headers for the request, etc).  Below the POST entry, I saw a warning/error message I had never seen before:
 This page has a file upload.  However, the form tag does not have both the enctype=multipart/form-data and method=POST attributes.  The file will not be uploaded.
Hmm...  I thought about it a bit, and then looked at a normal page I had that had file upload controls on it.  Sure enough, the form tag (there is always just one in ASP.NET) had the enctype attribute set automagically by ASP.NET if ASP.NET knew there was a file upload control on the page.  Otherwise, it leaves it off.  The Async update by the UpdatePanel was loading the FileUpload ok, but wasn't adding the enctype=multipart/form-data to the form tag attributes. 
Solution: manually add the enctype to the page's form on Page_Load()

                                                             
 Page.Form.Attributes.Add("enctype", "multipart/form-data"); 
                                                             

Source: http://blog.anofsinger.com/

No comments:

Post a Comment