I circumvented this issue by making the page invalid (so it wonβt be sent back) if the selected file exceeds the maximum request length. This requires the presence of a custom verification control for the client side, verification of the correctness of file upload control. The following is an example of server confirmation to limit the file size to 4 MB:
Sub custvalFileSize_ServerValidate(ByVal s As Object, ByVal e As ServerValidateEventArgs) 'FileUpload1 size has to be under 4Mb If (FileUpload1.PostedFile.ContentLength > 4194304) Then e.IsValid = False Else e.IsValid = True End If End Sub
Here is the client-side validation function:
function custvalFileSize_ClientValidate(src,args){ if (document.all("FileUpload1").files[0].size > 4194304){ args.IsValid = false; } else { args.IsValid = true; } }
Download and Validation Control:
<asp:FileUpload ID="FileUpload1" runat="server" /> <asp:CustomValidator ID="CustomValidator1" runat="server" BackColor="#FFFFFF" BorderColor="#FF0000" BorderStyle="solid" BorderWidth="0" ClientValidationFunction="custvalFileSize_ClientValidate" ControlToValidate="FileUpload1" Display="Dynamic" EnableClientScript="true" ErrorMessage="<b>Please upload document files of 4Mb or less.</b>" Font-Bold="false" Font-Names="Verdana, Arial, Helvetica" Font-Size="9px" ForeColor="#FF0000" OnServerValidate="custvalFileSize_ServerValidate" Text="<br/><span style='font-weight:bold;font-size:12px;'>This file is too large.<br />We can only accept documents of 4Mb or less.</span>"></asp:CustomValidator>
source share