473,624 Members | 2,642 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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

Similar topics

3
2635
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
2868
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 Fuzzy Software @ http://www.fuzzysoftware.com/ Comprehensive, categorised, searchable collection of links to ASP & ASP.NET resources...
3
1305
by: ypul | last post by:
Hi, am using httpmodule in my application. now <configuration> <system.web> <httpModules>
1
5666
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 (http://www.codeproject.com/aspnet/File_Upload_Progress_Bar.asp?df=100&forumid=155656#xx1079964xx) Here is the code I modified. The problem is: if I don't use guid in this function, it works fine. but if I get guid from querystring or form field, the file will not be uploaded and the while loop never...
3
2027
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 application on the server, a Windows 2003 box, the HTTPModule does not run. If I change the component to a simple dll and call its methods directly, the code works ok. As soon as I change the component back to an HTTPModule it stops working on the server....
1
1897
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 behavior. My module only runs for the extensions handled by the aspnet_isapi.dll. Here are my sequence of steps: 1) Create the HttpModule 2) Compile and drop dll in the webApp bin directory.
3
2175
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 declared of course into the Webconfig. This application runs perfect. So, i created another application (Accreditation), that runs perfect too inside of it as a virtual directory that does not use HttpModule. Now i am getting this error....
0
1387
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 files, however it may fail for any unpredictable reason. If it fails I want to invoke a context.response.redirect to the same url. This should result in a simple GET request which passes my module without further attention. I notice the following...
2
3172
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. the httpmodule is basically a simple implementation as all I've done so far is to implement required methods and to those I've added no code. the app builds but when I go to run it, it says the module type urlrewriter is ambiguous. it says it...
0
8246
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8179
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8685
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8341
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
5570
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4184
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2612
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1796
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1489
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.