Redirect to Error Page When Maximum Request Length is Exceeded [FileUpload]

 How many times you tried to catch the maximum request length error thrown by the FileUplad control if the web.config HttpRuntime entry is not modified?
In this blog post, Haissam Abdul Malak will create an HttpModule which check if the file is larger than 4 MB to redirect the user to a webform "Error.aspx".
The module will register to the BeginRequest event to execute the needed code


Below is the implementation

using System;
using System.Collections.Generic;
using System.Text;
using System.Web;


namespace HAMModule
{
    public class MyModule : IHttpModule
    {
        public void Init(HttpApplication app)
        {
            app.BeginRequest += new EventHandler(app_BeginRequest);
           
        }
        void app_BeginRequest(object sender, EventArgs e)
        {
            HttpContext context = ((HttpApplication)sender).Context;

            if (context.Request.ContentLength > 4096000)
            {
                IServiceProvider provider = (IServiceProvider)context;
                HttpWorkerRequest wr = (HttpWorkerRequest)provider.GetService(typeof(HttpWorkerRequest));

                // Check if body contains data
                if (wr.HasEntityBody())
                {
                    // get the total body length
                    int requestLength = wr.GetTotalEntityBodyLength();
                    // Get the initial bytes loaded
                    int initialBytes = wr.GetPreloadedEntityBody().Length;
         
                    if (!wr.IsEntireEntityBodyIsPreloaded())
                    {
                        byte[] buffer = new byte[512000];
                        // Set the received bytes to initial bytes before start reading
                        int receivedBytes = initialBytes;
                        while (requestLength - receivedBytes >= initialBytes)
                        {
                            // Read another set of bytes
                            initialBytes = wr.ReadEntityBody(buffer, buffer.Length);
                            // Update the received bytes
                            receivedBytes += initialBytes;
                        }
                        initialBytes = wr.ReadEntityBody(buffer, requestLength - receivedBytes);
                       
                    }
                }
  // Redirect the user to an error page.
                context.Response.Redirect("Error.aspx");
            }

        }
        public void Dispose()
        {
        }
    }
}

Now all you need to do is to add the module in the web.config file by adding the below entry under system.Web

 <httpModules>
        <add type="HAMModule.MyModule" name="MyModule"/>
 </httpModules>

Hope this helps,

Published 25 September 2008 11:01 AM by haissam

Comments

# StephanJade said on 25 October, 2009 08:46 AM

Great post you got here. It would be great to read more about this theme.

# File Upload « Petr Kotal's C# blog said on 30 September, 2011 07:34 AM

Pingback from  File Upload « Petr Kotal's C# blog

# Synthroid 225 mg. said on 14 November, 2011 12:04 PM

Synthroid. What is synthroid made of. Buy synthroid online without prescription.

This site

Search

Go

This Blog

Syndication

Sponsors

  • MaximumASP