Avatar of CompileError
CompileError asked on

Can't cancel user request during file upload.

Hello all!
I'm trying to create a kind of code which checks size of file being uploaded and if size exceeds a particular value it should cancel upload and redirect to a page containing error description. In Application_BeginRequest function of global.asax I simply check value of Request.ContentLength and try to redirect if it's bigger than expected by calling the Response.Redirect method. But then I'm getting an "Access is denied" javascript error in IE (in FireFox it just says that "connection to remote host is lost"). Could you please help me to find a solution? This error is making me crazy!
void Application_BeginRequest(object sender, EventArgs e)
	{
		HttpApplication app = sender as HttpApplication;
		if (app.Request.ContentLength > 10000)
		{
		    app.CompleteRequest();
		    Response.Redirect("~/Error.aspx");
		}
	}

Open in new window

ASP.NETWeb Applications

Avatar of undefined
Last Comment
CompileError

8/22/2022 - Mon
raterus

That's some weird syntax there "HttpApplication app = sender as HttpApplication;"  Have you tried just simply using "Application" as your variable, in replacement of "app"?  

on the note of what you're really trying to accomplish, I helped an expert over a year ago come to the conclusion that this just can't be done,
https://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/Q_22133562.html#a18890470

Perhaps things have changed, or you can find a better method?
ASKER
CompileError

Raterus, yes I've tried using this.Request and it didn't help. Besides, looks like "this" and "sender" reference to the same Application instance.
If it's not possible to cancel the request then how do other proprietary and free upload controls accomplish this task? For example, see NeatUpload control (http://www.brettle.com/neatupload#Download) - it allows you to cancel file upload at any moment. But I didn't understand how it works as I'm only a beginner in this sphere.
raterus

I wish I could tell you how those work too, my guess is they are going through a different chain of events than the standard <input type="file" /> but I don't know, sorry.
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
ASKER CERTIFIED SOLUTION
CompileError

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
ASKER
CompileError

Oops. I found that made a syntax error in code and that's why my handler had not been called. Now it works, so my solution works!