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.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
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.
Method 2
Add a trigger for your UpdatePanel
<Triggers> <asp:PostBackTrigger ControlID="btnUploadFile" /> </Triggers>
This will force a postback when the upload button is clicked.
Also add the line below to the Page_Load
Page.Form.Attributes.Add("enctype", "multipart/form-data");
Method 3
I know this post if old, but I found that if the file is empty [ 0 KB ] then it will return false as well. There has to be something in the file in order for .HasFile to acknowledge it.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0