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().G etProperty("WorkerRequest",(BindingFlags)36).GetVa lue(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(Htt pWorkerRequest.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.CurrentExecutionFileP ath;
current_page =current_page.Substring(current_page.LastIndexOf("/")+1) + "?" +application.Context.Request.QueryString;
application.Context.Response.Redirect("WebForm1.as px?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.
--------------------------------
From: Vitali Dzz
-----------------------
Posted by a user from .NET 247 (http://www.dotnet247.com/)
<Id>5/Y+nXRDFk+/rimtxEISJQ==</Id>