So what I am trying to do is, have a user select a file to upload. Since I am only going to accept images, I will test the extension. I also want to limit the file size to under 2mb, so I will test that(haven't implemented in code yet). If the file they have selected passes, then I want the label to say "File Accepted", and store the file upload information for a later button click. This will happen once the user has finished filling out the rest of the form. Eventually, I will put an UpdateProgress control on the page while it is checking if the file is allowed. I would rather not have it post back for this, so if I can get it to work, that would be great. BTW, this will all work fine if I take the label out of the update panel.
What happens when I run this, is it will go to the else statement of the first if and return "Please select a file." Meaning that FileUpload1.HasFile is returning false. The only reason I can see that this is happening is because the UpdatePanel can not access that info from the FileUpload control?
Code Behind:
Label SubmitButtonLabel2= (Label)UpdatePanel1.FindControl("SubmitButtonLabel");
if (FileUpload1.HasFile)
{
string[] fileName = FileUpload1.FileName.Split('.');
if ((fileName[fileName.Length - 1] == "jpg") ||
(fileName[fileName.Length - 1] == "gif") ||
(fileName[fileName.Length - 1] == "bmp") ||
(fileName[fileName.Length - 1] == "jpeg") ||
(fileName[fileName.Length - 1] == "png"))
{
SubmitButtonLabel2.Text = "File Accepted.";
}
else
{
SubmitButtonLabel2.Text = "File type not allowed. Please choose another.";
}
}
else
{
SubmitButtonLabel.Text = "Please select a file.";
}
Page:
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server" />
<div>
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="SubmitButton" runat="server" Text="Submit File" OnClick=SubmitButton_Click />
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="always">
<ContentTemplate>
<asp:Label ID="SubmitButtonLabel" runat="Server" />
</ContentTemplate>
<Triggers>
<asp:PostBackTrigger ControlID="SubmitButton" />
</Triggers>
</asp:UpdatePanel>
</div>
</form>
</body>
No need to do anything you just need to add multipart data attribute to your form.
Page.Form.Attributes.Add("enctype", "multipart/form-data");
See the following link for more details.
http://knowledgebaseworld.blogspot.com/2009/02/file-upload-not-working-with-update.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With