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(ob ject sender, EventArgs args)
{
// Create an instance of th application object
HttpApplication application = (HttpApplicatio n) sender;
try
{
// Create an instance of the HTTP worker request
HttpWorkerReque st request = (HttpWorkerRequ est)application .Context.GetTyp e().GetProperty ("WorkerRequest ",(BindingFlags )36).GetValue(a pplication.Cont ext, null);
// Only trigger if the request is of type 'multipart/form-data'
if(application. Context.Request .ContentType.In dexOf("multipar t/form-data") > -1)
{
// Check if a request body is sent by the browser
if(request.HasE ntityBody())
{
// Get the content length of the request
int content_length =Convert.ToInt3 2(request.GetKn ownRequestHeade r(HttpWorkerReq uest.HeaderCont entLength));
int content_receive d = 0;
// Get the preloaded buffered data
byte[] body = request.GetPrel oadedEntityBody ();
content_receive d += 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_leng th > 102400)
{
if(!request.IsE ntireEntityBody IsPreloaded())
{
// Create an input buffer to store the incomming data
byte[] a_buffer = new byte[16384];
int bytes_read = 16384;
while((content_ length - content_receive d) >= bytes_read)
{
bytes_read = request.ReadEnt ityBody(a_buffe r,a_buffer.Leng th);
content_receive d += bytes_read;
}
}
// Redirect to the page to avoid the browser hanging
string current_page =application.Co ntext.Request.C urrentExecution FilePath;
current_page =current_page.S ubstring(curren t_page.LastInde xOf("/")+1) + "?" +application.Co ntext.Request.Q ueryString;
application.Con text.Response.R edirect("WebFor m1.aspx?i=1");
}
}
}
}
catch(System.Th reading.ThreadA bortException e1)
{
}
catch(Exception exception)
{
object e = exception;
}
}
It works fine except 2 things:
1) When I try to put Response.Redire ct 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 "Theparamet er is incorrect". What can be done to make work thisproperly?
Thank's in advance.
--------------------------------
From: Vitali Dzz
-----------------------
Posted by a user from .NET 247 (http://www.dotnet247.com/)
<Id>5/Y+nXRDFk+/rimtxEISJQ==</Id>