Click here to Skip to main content
15,892,927 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
ASP.NET
<asp:FileUpload ID="FileUpload1" runat="server" />

C#
if (FileUpload1.HasFile)
 {
     string FileName = Path.GetFileName(FileUpload1.PostedFile.FileName);
     mail.Attachments.Add(new Attachment(FileUpload1.PostedFile.InputStream, FileName));
 }



Issue:

Fileupload contains null value even after selecting a file using fileupload control.
Posted
Comments
Sibasisjena 8-Jan-14 7:13am    
Please put a debugger in the if condition and upload one file again and check the the control is going inside the if block or not.
riodejenris14 8-Jan-14 7:21am    
thanx bro for your comment

1 solution

When a file is selected using FileUpload Control ,then on postback, PostedFile property gets initialized with HttpPostedFile object for the file. Since http request cannot maintain state, so it looses it's state.

use update panel before file upload control.
like:-
XML
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
      <ContentTemplate>
      <asp:FileUpload ID="fileUploadImage" 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>
 
Share this answer
 
Comments
riodejenris14 8-Jan-14 7:21am    
thanx its working!!!
Karthik_Mahalingam 8-Jan-14 7:23am    
hi riyaaz, if this is working, pls mark it as answer and close the post..
Karthik_Mahalingam 8-Jan-14 7:22am    
5,Good
TrushnaK 8-Jan-14 7:31am    
thanks muhhamad riyaaz....
TrushnaK 8-Jan-14 7:31am    
Thanks Karthik....

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900