Abhi Abhi - 2 months ago 19
ASP.NET (C#) Question

FileUpload1.HasFile is always returning false

I am uploading file using ASP.net File upload control.
My FileUpload1.HasFile is always returning false.

if (FileUpload1.HasFile)
{
DBOperations db = new DBOperations();

try
{
FileUpload1.SaveAs(Server.MapPath("~/uploadedImages/" + db.uploadImage(System.IO.Path.GetExtension(FileUpload1.FileName)) + System.IO.Path.GetExtension(FileUpload1.FileName)));
}
catch (Exception Ex)
{
String he = Ex.Message;
}
}


I am using following ASP.net Code

<asp:UpdatePanel ID="fileUpload" runat="server">
<ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="btnUploadFile" EventName="Click" />
</Triggers>
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="btnUploadFile" Text="Upload File" runat="server"
onclick="btnUploadFile_Click" />
<br />
<asp:RegularExpressionValidator ID="revImage" ControlToValidate="FileUpload1" ValidationExpression="^.*\.((j|J)(p|P)(e|E)?(g|G)|(g|G)(i|I)(f|F)|(p|P)(n|N)(g|G))$" Text="Invalid image type" runat="server" />

</ContentTemplate>
</asp:UpdatePanel>


I have tried even by removing AsyncPostBackTrigger and even by removing Whole asp:updatePanel then also my FileUpload1.HasFile always returns false.

Answer

ASP.NET's "AJAX" thing doesn't support file uploads in UpdatePanels out of the box. Change that trigger into a PostBackTrigger (which causes a full page load) or use something else to upload the file.