473,387 Members | 1,501 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

HttpModule and upload files

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>
Nov 19 '05 #1
0 1899

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: PJ | last post by:
Is it possible to capture a request as it hits the server and before the post data has been completely sent to the web server? Thanks~
3
by: parthivjoshi | last post by:
Do anyone have the idea how to add progressbar to the existing HTTPModule and HTTPApplication for File Upload. ********************************************************************** Sent via...
3
by: ypul | last post by:
Hi, am using httpmodule in my application. now <configuration> <system.web> <httpModules>
1
by: Li Zhang | last post by:
Hi guys, I am trying to write a httpmodule to implement upload file progress bar. I searched web and got some sample code from code project...
3
by: Erik Cruz | last post by:
Hi. I wrote an HTTPModule to log errors from my application. The component works well on all the development machines running Windows XP or Windows 2000 and VS.NET 2003. When I run the...
1
by: Faraz | last post by:
Hi everyone, I am running into a slight problem. My understanding is that a custom HttpModule will run for every request made to the server, regardless of the extension. I do not experience this...
3
by: Jose Fernandez | last post by:
HI first of all, excuse me for my english. And Thank in advance for even read my post. I have a problem that is driving me insane. I have an application (JUCAR) which use HttpModule and i have...
0
by: Andy | last post by:
Hi folks, I wrote a HttpModule to handle file uploads (on beginRequest) prior to the actual web form displaying the result. The file upload processing generally works very well also for large...
2
by: Jimi Schacht | last post by:
ok, so, i've got a web project in c#. I'm using IIS instead of dev server. The application builds fine until I add an httpmodule I intend to use for URL rewriting and it's entry in the web.config....
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.