int fileSize = FileUpload1.PostedFile.ContentLength; if (fileSize > (maxFileSize * 1024)) { PanelError.Visible = true; lblError.Text = "Filesize of image is too large. Maximum file size permitted is " + maxFileSize + "KB"; return; }
Select Your File <asp:FileUpload ID="FileUpload1" runat="server" /><br /> <asp:CustomValidator ID="CustomValidator1" runat="server" ErrorMessage="Please select valid .jpg or .jpeg file and less than 1MB" onservervalidate="CustomValidator1_ServerValidate"></asp:CustomValidator><br /> <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args){ string filename=""; if (FileUpload1.HasFile) { filename = FileUpload1.PostedFile.FileName; } else { args.IsValid = false; } if (filename == "") { // check file is empty or not args.IsValid = false; } //Check file less then 1MB multipy for 50*1024 Mb if ((FileUpload1.PostedFile.ContentLength / 1024) > 1024) { args.IsValid = false; } }