Search

Feb 19, 2009

File Upload not working with update panel

I was having problem while using File upload control which is inside the update panel and which was placed in user control. While doing post-back the value of PostedFile was blank, although page was properly valid.

Then I checked the Form property of Request object, the file upload control was missing in the AllKeys collection

We all know that if you want file uploads the form encode must be multipart/form-data. This is the reason why File Upload is not working properly inside Update Panel, lets understand this with following example.

Problem:

File upload is not working with update panel + asp.net

You are having two controls which needs to get display after one-another or post-back, first control does not have File upload and Second control does. Things to notice here is form tag will be reside either in ASPX page or Master page not in UserControl; and our File Upload is inside user control. Now when we load first control which does not have File Upload so the form will not encoded with multipart/form-data, now when you load second control which has the File Upload hence fort encode must be multipart/form-data, which is unfortunately not changing due to Update Panel.

Solution:

In problem definition we come to know that form must be encoded with multipart/form-data if we want to upload the file; solution is very simple, we just need to change encoded attribute of form with following single line!!!

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

49 comments:

Anonymous said...

excellent....worked well, thanks.

Anonymous said...

Thank you very much. It works flawlessly.

VicDo

Anonymous said...

Good work man...I am using Wizard control and my FileUpload control is in 3rd step and i was thinking that issue might have something to do with Wizard control and that was the case!....when it goes to next step, form encoding is not set...now i am setting in page load every time and it works great!!...thanks

Anonymous said...

Dude you rock!!!

Anonymous said...

Dude you rock!!!

Anonymous said...

Hi,
I am using fileupload control inside content page with update panel but it is not working yet with your solution.

Saravana Kumar said...

Thank u so much..you saved my day :)

Saravana Kumar said...

Thanks.. You saved my day dude. ;)

Anonymous said...

you solved my problem.. thx

Jalpesh Vadgama said...

Hi Imran,

Very nice post. It was very easy and use full. I am jalpesh and we met at Virutal tech days. Remember the entity framework guy.

Ashwani said...

hi
I am having still the same problem after

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

still i not able to upload the file in side the content page using FileUpload Control and if i use the same code out side the master page or in a fresh page the code works properly could you help me.
please reply soon
i am waiting... thank you

Angad Singh Khurana said...

Hey Man..
Thanks a lot for sharing this great piece of blog.you are a life saver man... thanks a ton....
Karunaker , Angad

Unknown said...

hey thanks for u r great knowledge

Anonymous said...

thanks, working great!!!

Anonymous said...

If you have an updatepanel you can also put a trigger on it to to a POST






......

this worked for me

Anonymous said...

Regarding using an upload control in a wizard.... This helped but the trick was I had accidently created the step for the upload control as a WizardStepTemplate instead of a WizardStep. Once I put the File Upload Control inside of Wizard Step 3, Then on Next (Goes to Step 4) I perform the upload and display the final it worked great!

Giacomo said...

YUO ARE GOD!

Anonymous said...

Thanks so much for this...

Anonymous said...

Great and simple, good job man.

Thanks so much.

sathishmncl said...

I think this solution might be perfect.
I tried this but it is not working.

Actually in which area we need to write the Page.Form.Attributes.Add("enctype", "multipart/form-data");
.
please explain.

Unknown said...

Marvelous solution...

Anonymous said...

very good ,,,,i works correctly

Unknown said...

Wow., Thank you so much . I am dying to find a solution since so many days.
You helped me a lot. It worked excellent .

Thanks a lot. You saved my time and my life.

Anonymous said...

excellent....
worked well
Thank You Very Much.

Anonymous said...

thanks for sharing useful work it helps me a lot

Anonymous said...

awosome solution

Anonymous said...

excellent!
It solved my problem

Prakash said...

Thanks for your help works fine

Anonymous said...

Thanks
But we need to for to implement this with this .
Step:1
Add Page.Form.Attributes.Add("enctype", "multipart/form-data")
in PageLoad

Step: 2 Add Your content as





Your Content....( )

Jhon said...

Thanks A LOT man ! I was going crazy with my uploads working only on second clic, and all the answers on the web leading to other stuff... Finally ended up on a stack overflow question pointing this page. Now it's solved !

Anonymous said...

its working. thanks

atozdevops said...

oh great..thaaks ...

Anonymous said...

Awwwsssuuuummmmmmmmm work..dude......

it's working........


thanxxxxxxxxxxx

Anonymous said...

I followed the way as u post... but unable to resolve the problem... Please somebody help me with this...

Anonymous said...

I have tried this, "enctype="multipart/form-data" is already in my form TAG but still does not work, any idea why?

Thanks

Anonymous said...

I did try as per your POST it did't work, may be I am doing something wrong, but I found one link which explain the reason of why update panel and file upload does not work together.
It worked well.

Please see below link:
http://www.aspsnippets.com/Articles/Using-FileUpload-Control-inside-ASP.Net-AJAX-UpdatePanel-Control.aspx

Anonymous said...

Thanks. BIg help

M S Nagendra said...

Thank you very much..
Really it saved for me lot of time.

Sandeep Tawaniya said...

It was not worked for me. I found another solution for this. I want to share with you

Please check this link :-
http://coding-spirit.blogspot.in/2012/11/fileupload-control-is-not-working.html

Sarfaraz said...

Thank you Imran Bhai.. u rock!!!

Rahul Sapkota said...

It didn't worked for me. :(

Rahul Sapkota said...

It didnt helped me.

i have written that code in page load event of master page and content page as well but it didnt worked.

Anonymous said...

Great. It worked. Thanks a lot

Gurdatta said...

Thank you very much.
It helped me a lot

Anonymous said...

Thanks a lot :-)

Nagin said...

How about if you want to Download ? I tried to do the same and it dint work

Rui Flexa said...

You saved my life!!! Thanks a lot!!!!!!!!

Unknown said...

Thanks a lot.....

Anonymous said...

Superb Dear...