Click here to Skip to main content
11,437,002 members (64,252 online)
Click here to Skip to main content

Tagged as

How to Maintain FileUpload Control’s State after PostBack

, 17 Aug 2010 CPOL
Rate this:
Please Sign up or sign in to vote.
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.

        //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)

Share

About the Author

Chirantan Upadhyay
Team Leader Interglobe Technologies
India India
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

 
QuestionHow can I create common method to maintain multiple fileupload control's state after postback Pin
raju@shekhar31-Mar-15 2:47
memberraju@shekhar31-Mar-15 2:47 
QuestionHow to Maintain RadAsyncUpload Control’s State after PostBack in telerik? Pin
Member 1129184616-Mar-15 1:23
memberMember 1129184616-Mar-15 1:23 
GeneralMy vote of 2 Pin
F-ES Sitecore5-Mar-15 1:32
professionalF-ES Sitecore5-Mar-15 1:32 
GeneralMy vote of 1 Pin
Member 1022936123-Dec-14 0:52
memberMember 1022936123-Dec-14 0:52 
GeneralRe: My vote of 1 Pin
Armand Szoke29-Jan-15 5:45
memberArmand Szoke29-Jan-15 5:45 
QuestionWorked for me Pin
sachin.vishwa9014-Dec-14 5:58
membersachin.vishwa9014-Dec-14 5:58 
GeneralMy vote of 1 Pin
yogesh199016-Sep-14 18:59
memberyogesh199016-Sep-14 18:59 
GeneralMy vote of 5 Pin
Rockstar_1-Aug-13 21:28
memberRockstar_1-Aug-13 21:28 
SuggestionThis is not a good idea Pin
Tore Olav Kristiansen8-Jul-13 0:23
memberTore Olav Kristiansen8-Jul-13 0:23 
GeneralRe: This is not a good idea Pin
Juju Shen8-Aug-13 0:22
memberJuju Shen8-Aug-13 0:22 
QuestionHow to use the code. Please explain Pin
mathewtinu21-Feb-13 0:05
membermathewtinu21-Feb-13 0:05 
QuestionThaks But.....! Pin
RobertDowney26926-Oct-12 22:50
memberRobertDowney26926-Oct-12 22:50 
GeneralMy vote of 5 Pin
Mlsoun17-Sep-12 14:01
memberMlsoun17-Sep-12 14:01 
GeneralRe: My vote of 5 Pin
Chirantan Upadhyay26-Sep-12 0:05
memberChirantan Upadhyay26-Sep-12 0:05 
GeneralRe: My vote of 5 Pin
mathewtinu21-Feb-13 0:06
membermathewtinu21-Feb-13 0:06 
GeneralMy vote of 5 Pin
Member 787890413-Sep-12 7:18
memberMember 787890413-Sep-12 7:18 
Simple solution
GeneralRe: My vote of 5 Pin
Chirantan Upadhyay26-Sep-12 0:06
memberChirantan Upadhyay26-Sep-12 0:06 
GeneralReason for my vote of 1 its not working... Pin
karthickbilla22-Feb-12 3:54
memberkarthickbilla22-Feb-12 3:54 
GeneralRe: Reason for my vote of 1its not working... Pin
Member 86976878-Apr-12 22:57
memberMember 86976878-Apr-12 22:57 
Generalwhere I paste this code Pin
Parveen Rathi24-Jan-12 9:01
memberParveen Rathi24-Jan-12 9:01 
GeneralRe: you can paste in page load event Pin
Chirantan Upadhyay8-Feb-12 3:03
memberChirantan Upadhyay8-Feb-12 3:03 
GeneralRe: you can paste in page load event Pin
mathewtinu21-Feb-13 0:07
membermathewtinu21-Feb-13 0:07 
GeneralIn the code, what is Label1? Pin
Michael B. Elkins5-Jan-12 4:08
memberMichael B. Elkins5-Jan-12 4:08 
GeneralRe: It's a label placed some where in your aspx page. this will ... Pin
Chirantan Upadhyay8-Feb-12 3:02
memberChirantan Upadhyay8-Feb-12 3:02 
Generalnot working in my condition. I hav Update Panel for a Page, ... Pin
monishafriends15-Nov-11 21:05
membermonishafriends15-Nov-11 21:05 
GeneralRe: not working in my condition.I hav Update Panel for a Page,... Pin
nagesh kumar verma18-Nov-13 4:53
membernagesh kumar verma18-Nov-13 4:53 
GeneralReason for my vote of 1 Reason: Storing FileUpload in Sessio... Pin
HaBiX15-Nov-11 1:20
memberHaBiX15-Nov-11 1:20 
Generalthanks... helped me a lot.... Pin
Jephunneh Malazarte2-Jun-11 1:24
memberJephunneh Malazarte2-Jun-11 1:24 
GeneralReason for my vote of 5 This is an excellent piece of inform... Pin
Member 232293624-Mar-11 2:47
memberMember 232293624-Mar-11 2:47 
GeneralReason for my vote of 5 Nice little way out for an annoying ... Pin
anilanand23-Mar-11 20:46
memberanilanand23-Mar-11 20:46 
GeneralReason for my vote of 5 This is something I am looking for o... Pin
Yatindra singh23-Mar-11 2:29
memberYatindra singh23-Mar-11 2:29 
GeneralRe: Thanks Pin
Chirantan Upadhyay23-Mar-11 2:46
memberChirantan Upadhyay23-Mar-11 2:46 
GeneralReason for my vote of 4 good Pin
hemal_kiran2-Feb-11 19:10
memberhemal_kiran2-Feb-11 19:10 
GeneralReason for my vote of 5 its helped me. nice trick and explai... Pin
shameerpv27-Sep-10 20:34
membershameerpv27-Sep-10 20:34 
GeneralThanx a lot Pin
shameerpv27-Sep-10 20:33
membershameerpv27-Sep-10 20:33 
GeneralReason for my vote of 4 its really good. Pin
anilkumarg84@gmail.com18-Aug-10 1:02
memberanilkumarg84@gmail.com18-Aug-10 1:02 
GeneralReason for my vote of 5 good trick Pin
Pranay Rana17-Aug-10 2:58
memberPranay Rana17-Aug-10 2:58 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150428.2 | Last Updated 17 Aug 2010
Article Copyright 2010 by Chirantan Upadhyay
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid