HttpModule and upload files

  • Thread starter Vitali Dzz via .NET 247
  • Start date
V

Vitali Dzz via .NET 247

Hello
I need some help
I need to write a code that analyze size of the uploaded file.

I wrote such HttpModule:

public void BeginRequest(object sender, EventArgs args)
{
// Create an instance of th application object
HttpApplication application = (HttpApplication) sender;

try
{
// Create an instance of the HTTP worker request
HttpWorkerRequest request = (HttpWorkerRequest)application.Context.GetType().GetProperty("WorkerRequest",(BindingFlags)36).GetValue(application.Context, null);

// Only trigger if the request is of type 'multipart/form-data'
if(application.Context.Request.ContentType.IndexOf("multipart/form-data") > -1)
{
// Check if a request body is sent by the browser
if(request.HasEntityBody())
{
// Get the content length of the request
int content_length =Convert.ToInt32(request.GetKnownRequestHeader(HttpWorkerRequest.HeaderContentLength));
int content_received = 0;

// Get the preloaded buffered data
byte[] body = request.GetPreloadedEntityBody();
content_received += body.Length;

// This is a nice feature to redirect users if they upload afile
// larger then 0,1Mb BEFORE it is being uploaded
if(content_length > 102400)
{
if(!request.IsEntireEntityBodyIsPreloaded())
{
// Create an input buffer to store the incomming data
byte[] a_buffer = new byte[16384];
int bytes_read = 16384;

while((content_length - content_received) >= bytes_read)
{
bytes_read = request.ReadEntityBody(a_buffer,a_buffer.Length);
content_received += bytes_read;
}
}
// Redirect to the page to avoid the browser hanging
string current_page =application.Context.Request.CurrentExecutionFilePath;
current_page =current_page.Substring(current_page.LastIndexOf("/")+1) + "?" +application.Context.Request.QueryString;
application.Context.Response.Redirect("WebForm1.aspx?i=1");
}
}
}
}
catch(System.Threading.ThreadAbortException e1)
{
}
catch(Exception exception)
{
object e = exception;
}
}

It works fine except 2 things:

1) When I try to put Response.Redirect just after the sizecomparison on client window I see page
"Action cancelled". So I need to wait while the whole file isuploaded and then make Redirect. Can I avoid uploading the wholeFile?
2) In this form it works so: when a user try to upload file morethan 0,1 Mb size it's redirect to the same page with warning. Atfirst time it works fine. After user selected another file withsize more than 0,1 Mb the page he see contains only message "Theparameter is incorrect". What can be done to make work thisproperly?

Thank's in advance.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top