Click here to Skip to main content
14,838,228 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have form where I need to upload file and click on submit. If I try to upload a file of size more than 4095 bytes it going to an server error page

Server Error in '/' Application.

Maximum request length exceeded.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Web.HttpException: Maximum request length exceeded.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:


[HttpException (0x80004005): Maximum request length exceeded.]
System.Web.HttpRequest.GetEntireRawContent() +9726852
System.Web.HttpRequest.GetMultipartContent() +63
System.Web.HttpRequest.FillInFormCollection() +165
System.Web.HttpRequest.EnsureForm() +75
System.Web.HttpRequest.get_Form() +12
System.Web.HttpRequest.get_HasForm() +9728403
System.Web.UI.Page.GetCollectionBasedOnMethod(Boolean dontReturnNull) +95
System.Web.UI.Page.DeterminePostBackMode() +69
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +130

Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.34009


The above is the error I receive in browser when I try to upload file of larger size. I haven't specified the maximum size to be uploaded for the file but still I am getting getting the error. I also tried using try catch block to catch the exception but I am getting this error. I even tried to extend the default file size in ASP.NET using web.config and still it is not working. Can anyone please help me to fix this issue.

HTML
protected bool Save()
    {
        bool retVal = false;
        School thisSchool = null;
        SchoolService schoolsService = new SchoolService();
        
        if (SessionContents.CurrentSchool != null)
        {
            thisSchool = SessionContents.CurrentSchool;
        }
        else
        {
            return false;
        }
        DocumentRepository thisDocumentRepository = new DocumentRepository();

        if (thisDocumentRepository.SchoolRootFolderExists(thisSchool))
        {
           //Check if logo needs saving, i.e. if file upload has a file
            if (uxFileUpload.FileBytes.Length > 0)
            {
                 try
           {
                int fileSize = uxFileUpload.FileBytes.Count();

                string fileExt = System.IO.Path.GetExtension(uxFileUpload.FileName);
                fileExt = fileExt.Replace(".", "");

                if (!thisDocumentRepository.ValidSupportingDocument(fileExt))
                {
                    retVal = false;
                    string allowedDocType = thisDocumentRepository.AllowedFileTypeList();
                    
                    CustomValidator err = new CustomValidator();
                    err.ValidationGroup = "SaveFile";
                    err.IsValid = false;
                    err.ErrorMessage = "You did not select a valid file for this screen. Please only upload files of type: " + allowedDocType;
                    //if (fileSize > 1048576)
                    //{
                    //    err.ErrorMessage = "The maximun size has been extended";
                    //}
                    Page.Validators.Add(err);

                }
                
                else
                {
                    if (this.ObjectID == -1)
                    {
                        //save parent record stub
                        switch (this.DocumentFunctionalObjectType)
                        {
                            case DocumentRepository.DocumentFunctionalObjectType.Consultation:

                                break;

                            case DocumentRepository.DocumentFunctionalObjectType.Issue:

                                break;

                            case DocumentRepository.DocumentFunctionalObjectType.Initiative:

                                break;

                            case DocumentRepository.DocumentFunctionalObjectType.Activity:

                               break;

                            case DocumentRepository.DocumentFunctionalObjectType.Star:

                               if (SessionContents.SelectedAcademicYear != null)
                               {
                                   
                               }
                                break;

                            case DocumentRepository.DocumentFunctionalObjectType.YtaSignOff:

                                if (SessionContents.SelectedAcademicYear != null)
                                {
                                    SignOffService signOffsService = new SignOffService();
                                    SignOff newSignOff = signOffsService.CreateNewObject();

                                    newSignOff.SchoolID = thisSchool.SchoolID;
                                    newSignOff.AccreditationTypeID = 4;
                                    newSignOff.AcademicYearID = SessionContents.SelectedAcademicYear.AcademicYearID;
                                    newSignOff.YtaTravelPlan = true;
                                    newSignOff.Submitted = false;
                                    newSignOff.SignedOffBySchool = false;
                                    newSignOff.SignedOffByBorough = false;
                                    newSignOff.SignedOffByTfl = false;

                                    signOffsService.Save(newSignOff);

                                    this.ObjectID = (int)newSignOff.SignOffID;
                                }
                                break;

                            case DocumentRepository.DocumentFunctionalObjectType.TravelPlan:
                                
                                break;

                            case DocumentRepository.DocumentFunctionalObjectType.CaseStudy:

                                break;
                        }
                    }
                    if (this.ObjectID != -1)
                    {
                        thisDocumentRepository.SaveSupportingDocument(uxFileUpload, this.DocumentFunctionalObjectType, thisSchool, this.ObjectID);
                        retVal = true;
                    }
                }
           }
                 catch (Exception ex)
                 {
                     System.Console.WriteLine("You have not specified a file.");
                     
                 }
            }
                   
          
            else
            {

                retVal = true;
            }
        }
        return retVal;
    }
Posted
Updated 24-Mar-15 4:17am
v2

1 solution

What are you trying to fix? Are you trying to allow bigger files? Or not get the error? For bigger files if you google the error message you'll find this

http://www.smarterasp.net/support/kb/a1544/how-to-resolve-maximum-request-length-exceeded.aspx[^]

For not getting the error at all I'm afraid that exception fires before your code runs so you can't use try\catch, you'll have to implement a custom 500 error page.

You might also want to use the html5 file api to check the size of the file before it is uploaded for those browsers that support html5.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900