Click here to Skip to main content
15,861,168 members
Articles / Web Development / ASP.NET
Tip/Trick

How to Maintain FileUpload Control’s State after PostBack

Rate me:
Please Sign up or sign in to vote.
4.86/5 (35 votes)
17 Aug 2010CPOL1 min read 303.6K   13   44
How to Maintain FileUpload Control’s State after PostBack

Introduction


Once I was trying to create a page it has two file upload controls and many other fields and around 30 post backs. Once user select files in file upload controls and in any selection when page’s post back event occurs, file upload control loose it’s value. It was very annoying situation for me to maintain the state of FileUpload Control. I search around so many articles and sites but didn't get any fixed solution. Then I decided to do it myself and publish for other developers. This is very common problem with many developers.


About ASP.Net's File Upload Control


File upload control has a text box and a browse button. When a file is selected, on PostBack PostedFile property gets initilized with HttpPostedFile object for the file. As we know that http request can't maintain state, and PostedFile is initilized with HttpPostedFile Object so it loose it's state. PostedFile property can be used to get information about file e.g. ContentLength, Content Type, FileName, InputStream. It has also a method SaveAs to store file on disk. You can read more about it on MSDN. To maintain the length of solution I will focus to code snippet.


How do we Achieve


I have seen so many options available, but many of them are not working or need too much effort. How can we do this with minimal efforts? Idea is Session Object. As we know session object can store any object and in case of OutProc/State Server Serializable Object only. So idea is very simple store fileupload object in session and in the post back get the values back from it. Use this code in Page_Load.


C#
//If first time page is submitted and we have file in FileUpload control but not in session
// Store the values to SEssion Object
if (Session["FileUpload1"] == null && FileUpload1.HasFile)
{
    Session["FileUpload1"] = FileUpload1;
    Label1.Text = FileUpload1.FileName;
}
// Next time submit and Session has values but FileUpload is Blank
// Return the values from session to FileUpload
else if (Session["FileUpload1"] != null && (! FileUpload1.HasFile))
{
    FileUpload1 = (FileUpload) Session["FileUpload1"];
    Label1.Text = FileUpload1.FileName;
}
// Now there could be another sictution when Session has File but user want to change the file
// In this case we have to change the file in session object
else if (FileUpload1.HasFile)
{
    Session["FileUpload1"] = FileUpload1;
    Label1.Text = FileUpload1.FileName;
}

Conclusion


This is very simple and effort less solution. Does not require any extra effort.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Team Leader Interglobe Technologies
United States United States
Hi I am chirantan upadhyay. I always like to do R&D on MS Technology and search newer and newer things. I always think something typical then try to make that possible in Code. I am B.Sc.,C-DAC,MCTS-Sharepoint,Brainbench ASP.Net and Brainbench C# Certified.
I like to have fun in Nature and Adventurous things like river rafting, rock climbing, and rippling.

Comments and Discussions

 
QuestionDoesn't work Pin
DLChambers18-Sep-20 1:54
DLChambers18-Sep-20 1:54 
SuggestionVery bad Logic Pin
Member 1371264425-Jun-20 2:33
Member 1371264425-Jun-20 2:33 
QuestionLoosing the session value Pin
Member 1188452110-Sep-18 20:35
Member 1188452110-Sep-18 20:35 
QuestionCan it work with VB.NET? Pin
Jeff Levin24-Apr-17 12:51
Jeff Levin24-Apr-17 12:51 
QuestionHow can I create common method to maintain multiple fileupload control's state after postback Pin
raju@shekhar31-Mar-15 1:47
raju@shekhar31-Mar-15 1:47 
QuestionHow to Maintain RadAsyncUpload Control’s State after PostBack in telerik? Pin
Prem S16-Mar-15 0:23
Prem S16-Mar-15 0:23 
GeneralMy vote of 2 Pin
F-ES Sitecore5-Mar-15 0:32
professionalF-ES Sitecore5-Mar-15 0:32 
GeneralMy vote of 1 Pin
Member 1022936122-Dec-14 23:52
Member 1022936122-Dec-14 23:52 
GeneralRe: My vote of 1 Pin
ArmyBlond29-Jan-15 4:45
ArmyBlond29-Jan-15 4:45 
GeneralRe: My vote of 1 Pin
Hornwood5098-Mar-17 4:13
Hornwood5098-Mar-17 4:13 
AnswerRe: My vote of 1 Pin
ArmyBlond1-Jun-17 4:26
ArmyBlond1-Jun-17 4:26 
QuestionWorked for me Pin
sachin.vishwa9014-Dec-14 4:58
professionalsachin.vishwa9014-Dec-14 4:58 
AnswerRe: Worked for me Pin
Mohit Salecha11-Jan-16 23:49
Mohit Salecha11-Jan-16 23:49 
GeneralMy vote of 1 Pin
yogesh199016-Sep-14 17:59
yogesh199016-Sep-14 17:59 
GeneralMy vote of 5 Pin
Rockstar_1-Aug-13 20:28
professionalRockstar_1-Aug-13 20:28 
SuggestionThis is not a good idea Pin
Tore Olav Kristiansen7-Jul-13 23:23
Tore Olav Kristiansen7-Jul-13 23:23 
GeneralRe: This is not a good idea Pin
Juju Shen7-Aug-13 23:22
Juju Shen7-Aug-13 23:22 
QuestionHow to use the code. Please explain Pin
mathewtinu20-Feb-13 23:05
mathewtinu20-Feb-13 23:05 
QuestionThaks But.....! Pin
RobertDowney26926-Oct-12 21:50
RobertDowney26926-Oct-12 21:50 
GeneralMy vote of 5 Pin
Mlsoun17-Sep-12 13:01
Mlsoun17-Sep-12 13:01 
GeneralRe: My vote of 5 Pin
Chirantan Upadhyay25-Sep-12 23:05
Chirantan Upadhyay25-Sep-12 23:05 
GeneralRe: My vote of 5 Pin
mathewtinu20-Feb-13 23:06
mathewtinu20-Feb-13 23:06 
GeneralMy vote of 5 Pin
Member 787890413-Sep-12 6:18
Member 787890413-Sep-12 6:18 
GeneralRe: My vote of 5 Pin
Chirantan Upadhyay25-Sep-12 23:06
Chirantan Upadhyay25-Sep-12 23:06 
GeneralReason for my vote of 1 its not working... Pin
karthickbilla22-Feb-12 2:54
karthickbilla22-Feb-12 2:54 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.