Maintain State For ASP.Net FileUpload After PostBack

Posted by Sharpcnet under ASP.NET on 11/5/2013 | Points: 10 | Views : 37361 | Status : [Member] | Replies : 8
The filename does not stay in Fileupload control's textbox, after postback. I've tried using Session object but it still doesnt work.
ASPX:
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:FileUpload ID="fileupload1" runat="server"></asp:FileUpload>
<asp:Button ID="btnUpload" runat="server" Text="Upload Image"
OnClick="btnUpload_Click" />
</ContentTemplate>
<Triggers>
<asp:PostBackTrigger ControlID="btnUpload" />
</Triggers>
</asp:UpdatePanel>
CS:

protected void Page_Load(object sender, EventArgs e)
{
if (Session["FileUpload1"] == null && FileUpload1.HasFile)
{
Session["FileUpload1"] = FileUpload1;
}
else if (Session["FileUpload1"] != null && (! FileUpload1.HasFile))
{
FileUpload1 = (FileUpload) Session["FileUpload1"];
}
else if (FileUpload1.HasFile)
{
Session["FileUpload1"] = FileUpload1;
}
}

protected void btnUpload_Click(object sender, EventArgs e)
{
if (fileupload1.HasFile)
{
fileUpload1.SaveAs(Server.MapPath("~/UploadedContent/Temp.jpg"));
img.ImageUrl = "~/UploadedContent/Temp.jpg";
FileUpload1 = (FileUpload) Session["FileUpload1"];
}
}
I am able to preview the image onclick, but the filename does not appear anymore after that, in the fileupload control. How can I achieve this.




Responses

Posted by: Bandi on: 11/5/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
//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;
}

References:
http://www.dotnetspider.com/forum/325894-Save-filename-fileupload-after-postback.aspx
http://www.codeproject.com/Tips/101834/How-to-Maintain-FileUpload-Control-s-State-after-P
http://forums.asp.net/t/1601068.aspx

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Sharpcnet, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 11/5/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
http://stackoverflow.com/questions/18651039/persist-fileupload-control-value

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Sharpcnet, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sharpcnet on: 11/5/2013 [Member] Starter | Points: 25

Up
0
Down
Hi, How was this different from my code. Anyways, exactly followed the same, but no luck. Updated my question with the changes. I am expecting the name of the file to show up in the fileupload control's textbox. Is it possible.

Sharpcnet, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 11/5/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
try this..........

    <form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<asp:Image ID="img" runat="server"/>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" >
<ContentTemplate>
<asp:FileUpload ID="FileUploadControl" runat="server" EnableViewState="True" ViewStateMode="Enabled" />
</ContentTemplate>

<Triggers>
<asp:PostBackTrigger ControlID="btnPreview" />
</Triggers>
</asp:UpdatePanel>
<asp:UpdatePanel ID="UpdatePanel2" runat="server" >
<ContentTemplate>
<asp:Button ID="btnPreview" runat="server" Text="Preview" OnClick="btnPreview_Click" ViewStateMode="Enabled"/>
</ContentTemplate>
</asp:UpdatePanel>
<asp:Label Text="" runat="server" ID="Label1" />
</div>
</form>

        protected void Page_Load(object sender, EventArgs e)
{
// store the FileUpload object in Session.
// "FileUpload1" is the ID of your FileUpload control
// This condition occurs for first time you upload a file
if (Session["FileUpload1"] == null && FileUploadControl.HasFile)
{
Session["FileUpload1"] = FileUploadControl;
Label1.Text = FileUploadControl.FileName; // get the name
}
// This condition will occur on next postbacks
else if (Session["FileUpload1"] != null && (!FileUploadControl.HasFile))
{
FileUploadControl = (FileUpload)Session["FileUpload1"];
Label1.Text = FileUploadControl.FileName;
}
// when Session will have File but user want to change the file
// i.e. wants to upload a new file using same FileUpload control
// so update the session to have the newly uploaded file
else if (FileUploadControl.HasFile)
{
Session["FileUpload1"] = FileUploadControl;
Label1.Text = FileUploadControl.FileName;
}
}

protected void btnPreview_Click(object sender, EventArgs e)
{

if (FileUploadControl.HasFile)
{

Session["fileUpload"] = FileUploadControl;

FileUploadControl.SaveAs(Server.MapPath("~/Temp.jpg"));

img.ImageUrl = "~/Temp.jpg";

FileUploadControl = (FileUpload)Session["fileUpload"];

}

}




Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Sharpcnet, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sharpcnet on: 11/5/2013 [Member] Starter | Points: 25

Up
0
Down
No Luck,..I can preview the image, but the file control still says 'No file chosen'...
Is it possible to use a hidden field to store and bind the file name to the fileupload (in c#)?



Sharpcnet, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 11/5/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
Even I also tried with hidden field.. But FileUpload.FileName is Read only.. So you can't assign value to it..

Refer this link and try one more attempt
http://www.gchandra.com/web/ajax/how-to-preserve-fileupload-value-in-aspnet.html
http://stackoverflow.com/questions/5326007/assign-file-to-be-uploaded-path-to-fileupload-control-after-postback

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Sharpcnet, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: vishalneeraj-24503 on: 11/5/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
http://www.aspsnippets.com/Articles/Using-ASP.Net-AJAX-Control-Toolkits-AsyncFileUpload-Control.aspx

Sharpcnet, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sharpcnet on: 11/5/2013 [Member] Starter | Points: 25

Up
0
Down
Wonder why they aint work for me. And ReadOnly....? Lost all hope then :(. Thanks a lot for your time though.

Sharpcnet, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response