I am using ASP.net FileUpload control to upload multiple files in database.
<asp:UpdatePanel ID="UP_div_askQ" UpdateMode="Conditional" runat="server">
<ContentTemplate>
<asp:FileUpload ID="FUQuestionFiles" CssClass="form-control" AllowMultiple="true" runat="server" />
<asp:Button ID="btnQSave" runat="server" CssClass="btn btn-success" Text="ASK QUESTION" OnClick="askQuestion" />
</ContentTemplate>
<Triggers>
<asp:PostBackTrigger ControlID="btnQSave" />
</Triggers>
</asp:UpdatePanel>
In code behind I am using HasFile
to check if files are present or not.
log.Debug("there are file" + FUQuestionFiles.HasFile.ToString());
But HasFile
returns false when more than one file is selected.
Note: In case where only one file is selected, it returns true.
Thanks for your help.
FileUpload has two different properties for checking if a/any file has uploaded:
FileUpload.HasFile:
Gets a value indicating whether the FileUpload control contains a file.
FileUpload.HasFiles:
Gets a value that indicates whether any files have been uploaded.
The best way to check if any files is uploaded is to check both HasFile
and HasFiles
together.
if(fileUpload1.HasFile || fileUpload1.HasFiles)
// do some code!
Edit 1:
Have you tried to add line below in the Page_Load
?
Page.Form.Attributes.Add("enctype", "multipart/form-data");
Edit 2:
Can you please explain what difference this line of code made?
From this Forms in HTML documents draft in W3C:
The content type "application/x-www-form-urlencoded" is inefficient for sending large quantities of binary data or text containing non-ASCII characters. The content type "multipart/form-data" should be used for submitting forms that contain files, non-ASCII data, and binary data.
If the form contains a file input
the enctype
attribute of the form should set to multipart/form-data
.
I think you faced that problem because you put the FileUpload
inside the UpdatePanel
.