File size limit: System.Web.HttpException

Discussion in 'ASP.NET / ASP.NET Core' started by Steve, Dec 15, 2014.

  1. Hello

    I have an file upload button on an aspx file. The file size limit, defined in Web.config is: 500Kb. When I deliberately go over that amount to test it, I get the following error:

    System.Web.HttpException: Maximum request length exceeded.

    Instead of sending the user to that page, how would I 'trap' his 'misdemeanour' and show an error message on the page?

    This is what should show the error on screen in my aspx file:

    Code:
    <asp:labelid="messageArea" runat="server"></asp:Label>
    and this is in my aspx.vb file:

    Code:
    ProtectedFunction IsFileValid(uploadControl AsHtmlInputFile) AsBoolean
    Dim isFileOk AsBoolean = True
    If uploadControl.PostedFile IsNotNothingThen
    Dim isSizeError AsBoolean = False
    Dim isExtensionError AsBoolean = False
    ' Validate for size less than MaxSizeAllowed...
    If uploadControl.PostedFile.ContentLength > MaxSizeAllowed Then
    isFileOk = False
    isSizeError = True
    Else
    isFileOk = True
    isSizeError = False
    EndIf
    ' If OK so far, validate the file extension...
    If isFileOk Then
    isFileOk = False
    isExtensionError = True
    ' Get the file's extension...
    Dim fileExtension AsString = System.IO.Path.GetExtension(uploadControl.PostedFile.FileName).ToLower()
    For i AsInteger = 0 ToMe.AllowedExtensions.Length - 1
    If fileExtension.Trim() = Me.AllowedExtensions(i).Trim() Then
    isFileOk = True
    isExtensionError = False
    Exit For
    EndIf
    Next
    EndIf
    If isSizeError Then
    Dim errorMessage AsString = "The size of the file " & Convert.ToString(uploadControl.PostedFile.FileName) & " is bigger than the allowed " & MaxSizeAllowed & " (" & Convert.ToString(uploadControl.PostedFile.ContentLength) & ").<br>"
    'Me.messageArea.Text = errorMessage
    messageArea.Text = errorMessage
    messageArea.Visible = True
    EndIf
    If isExtensionError Then
    Dim errorMessage AsString = "Your file " & Convert.ToString(uploadControl.PostedFile.FileName) & " cannot be uploaded.<br>File types allowed: .doc, .docx, .txt, .png, .gif, .bmp, .jpg, or .jpeg.<br>"
    messageArea.Text = errorMessage
    messageArea.Visible = True
    EndIf
    EndIf
    Return isFileOk
    messageArea.Attributes.Add("style", "display:block")
    EndFunction
    Thank you for your time.
     
  2. RayH

    RayH DiscountASP.NET Lackey DiscountASP.NET Staff

    mjp likes this.
  3. Hello Ray

    Like this:

    Code:
    Try
    Catch ex AsException
    If isSizeError Then
    Dim errorMessage AsString = "The size of the file " & Convert.ToString(uploadControl.PostedFile.FileName) & " is bigger than the allowed " & MaxSizeAllowed & " (" & Convert.ToString(uploadControl.PostedFile.ContentLength) & ").<br>"
    'Me.messageArea.Text = errorMessage
    messageArea.Text = errorMessage
    messageArea.Visible = True
    EndIf
    EndTry
     
  4. RayH

    RayH DiscountASP.NET Lackey DiscountASP.NET Staff

    Sorry, I'm not a VB expert, but here's pseudo-code:

    Code:
    Try
    ' Code to upload file
    Catch ex AsException
    If ex IsNot Nothing
    ' Do something like show an error
    EndIf
    EndTry
     
  5. Thanks for that, Ray.

    The problem turned out to be a bit larger than I expected!

    Thanks again for your help.
     
  6. mjp

    mjp

    What was the problem?
     
  7. It related to my Web.config files and the sitemap - I'm still not sure it's right. I will have a look at it again and pinpoint the exact error and post back, if that's OK.
     

Share This Page