Last post Jul 11, 2011 03:00 AM by manishgvsharma2000
Jul 10, 2011 08:19 AM|suzyb|LINK
I have an httpmodule to catch errors when people try to upload too large a file. But I am getting a "Value does not fall within the expected range" error when trying to redirect to an error page. This is my BeginRequest code.
HttpRuntimeSection runTime = (HttpRuntimeSection)WebConfigurationManager.GetSection("system.web/httpRuntime");
//Approx 100 Kb(for page content) size has been deducted because the maxRequestLength proprty is the page size, not only the file upload size
int maxRequestLength = (runTime.MaxRequestLength - 100) * 1024;
//This code is used to check the request length of the page and if the request length is greater than
//MaxRequestLength then retrun to the same page with extra query string value action=exception
HttpContext context = ((HttpApplication)sender).Context;
if (context.Request.ContentLength > maxRequestLength)
IServiceProvider provider = (IServiceProvider)context;
HttpWorkerRequest workerRequest = (HttpWorkerRequest)provider.GetService(typeof(HttpWorkerRequest));
// Check if body contains data
// get the total body length
int requestLength = workerRequest.GetTotalEntityBodyLength();
// Get the initial bytes loaded
int initialBytes = 0;
if (workerRequest.GetPreloadedEntityBody() != null)
initialBytes = workerRequest.GetPreloadedEntityBody().Length;
byte buffer = new byte;
// Set the received bytes to initial bytes before start reading
int receivedBytes = initialBytes;
while (requestLength - receivedBytes >= initialBytes)
// Read another set of bytes
initialBytes = workerRequest.ReadEntityBody(buffer, buffer.Length);
// Update the received bytes
receivedBytes += initialBytes;
initialBytes = workerRequest.ReadEntityBody(buffer, requestLength - receivedBytes);
Does anyone know what could be wrong. The only thing I can find on google is IIS rejecting new lines in headers. But I still get the error when I clear the headers.
btw I'm using umbraco if that makes a difference.
Jul 10, 2011 01:24 PM|manishgvsharma2000|LINK
see this example
you can define some size and then give custom error.
Jul 10, 2011 02:48 PM|suzyb|LINK
That uses flash so we can't use it.
Jul 11, 2011 03:00 AM|manishgvsharma2000|LINK
check this , you can rectrict max file size and give error , let me know if you need more information