Monday, October 29, 2007

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");

 
Technorati Tags: , ,

53 comments:

Anonymous said...

Thanks Adam! This was a great timesaver!

Lee

Tim said...

Thanks for posting this - I've been trying to figure this one out myself for the past week with no success.

Adam Nofsinger said...

Happy to help! :-)

Anonymous said...

I thank you from the bottom of my geeky heart for saving my entire week from despair trying to fix this damn bug!

el.MuTTkiD said...

Thanks for saving us time with your solution, i was missing the "enctype" attribute and getting this error, at last FileUpload working (from the first postback) on the update panels =D

Anonymous said...

thanks muchly

Vijay Balyan said...

Hi Adam,

You are the man!!!

I think thanks is not just enough for you.

You cannot imagine what you have done. You saved us from a great problem. You are a genius absolutely.

Great Timesaver!

Keep it up!

manoj said...

hello adam ,
i just wanna to say that i try this code at the page load.
and now it's work prpperly.
thanks for ur great suggestion
manoj shukla

Anonymous said...

Thankyou very, very much! :)

Anonymous said...

Very thanks!

Rayvanth said...

thanks dude it helped me a lot....

laxmi said...

Thanks man, you save my life.
it's great for me.

Anonymous said...

Thanks,
This problem is looking small ,
but it was much time consumer.

Thanks for you solution.

Chris Kirkman said...

Way to go man. You saved my ass. I had to deliver by tomorrow and you got me done in time. This was killing me.

Suvabrataroy said...

Thanks Adam but I have another problem, that is when ever my web page post back the css will be disabled automatically only in IE 7.

Thanks In Advance

Adam Nofsinger said...

Does this help?
http://stackoverflow.com/questions/481308/asp-net-css-on-postback-in-ie-7-not-working

Scott said...

Still helping! Thanks!

Mattia said...

A very big THANK YOU !! I've spent one day and a half trying the solution to this problem !!

Thanyou said...

excellent ...simply added code to pageLoad event and all fixed !!!

iscaglia said...

It also works when the FileUpload is in a UserControl, and this in turn is being used on the page. Tks!

Gkrishnan84 said...

Thanks it helped

Pkalpsh said...

greate thanks

santhakumar said...

THANKS LOTS HAVE NO WORDS TO THANK YOU

santhakumar said...

It is absolutly Rocking boss

Hemant Yadav said...

Thanks a lot it has solved my problem.

Arclous said...

 thanks a lot, i spent a lot of time for this problem.

Dave said...

Stunning!  That ends a four hour debugging battle from hell.... :-)

Petr Kononov said...

Thanks!

Bastin said...

Wounderful..

Oaky_21 said...

Thanks your a life saver

new1one said...

Thank you Adam, this is great!!!

Msbyuva said...

Simple..Great ..!!

Archana Pradhan said...

Thanks  a lot for this solution.It works for me..

Srinivasmadduri said...

Thanks alot its excelent code,

Francisco Arenas said...

Thanks a lot. Just a line for solve a big problem.

sanjeevmanna said...

thank you very much...its 100% solved my problems..

Xyz123 said...

Thanks.It works fine for me.

Sincil said...

Wow wow! That good one

Cjonas said...

wow your awesome... Spend hours trying to figure this one out.  ++

Contacttorohan said...

tnx

Amit said...

Shoutout from Calgary, Canada. Thanks for sharing. Solved the issue for us.

Rafi said...

Thanks.It works fine .

Avinash said...

its great for me ........
thanks

Ganesh Magar said...

This works absolutely fine. thanks!

veereshd513 said...

I am not getting

Imran said...

Great.... It worked for me...
Thank you so much :)

Vasil said...

nice and clean, you are great man! :)

Raj said...

Excellent Great

Raj said...

thanka a lot

soumi said...

thank you...was stuck for this from 1 hr

kausha said...

hello...can you give me demo source code for file upload because

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

line is not working in my application.

i will wait for your favorable replay,

Thank-you.

DDC said...

Thank you :)

shivkumar s p said...

thanku it works

Disqus for A Nofsinger's Blog