file upload maximum allowed size error handling

J

J055

Hi

How do I tell the user he has tried to upload a file which is too big...

1. when the httpRuntime.maxRequestLength has been exceeded and
2. when the uploaded file is under then httpRuntime.maxRequestLength


For point 1. it would be good to at least display a nice error page. IE
seems to just display a blank page.


Thanks
Andrew
 
K

Ken Cox [Microsoft MVP]

There's a suggested workaround here that might be worth a try:

http://blog.hackedbrain.com/archive/2006/03/13/3860.aspx


"1. Create a global.asax file. If you're using Visual Studio 2005 it will
set up a number of common subroutines for you. You need to use Sub
Application_Error. Your code should look something like this:

<script runat="server">
Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
Dim currentException As Exception
currentException = Server.GetLastError.GetBaseException()
Response.Redirect("/error.aspx?Err=" &
Server.UrlEncode(currentException.Message))
End Sub
</script>

The application_Error sub fires as a last resort, in other words, when you
haven't explicitly handled the exception anywhere else in your code. "

2. You can now create an error.aspx that displays the exception message
(from the querystring). For this exception the message is "Maximum request
length exceeded". You could also test for the message and give users more
information on the error.
 
S

Steven Cheng[MSFT]

Hi Ken,

Thanks for your input.

However, as for the Application_Error approach in the article you
mentioned, have you ever tried it in some test applications? Based on my
experience, this is not guaranteed to work since when the uploading data
exceed the ASP.NET httpRuntime maxRequestLength, the runtime will directly
stop the worker thread and close the connection, thereforce any sequential
exception handler code has no chance to be executed. That's also why we
generally will see a "server not found ..." error page(like a connection
issue).

Hi Andrew,

I'm afraid this is a tough issue for ASP.NET large file uploading. As I
mentioned above, the maxRequestLengh exceeding exception will close the
underlying tcp/http socket connection that cause the seqential error
handling code unable to get executed. I've ever met some other customers
discussing on this issue and have also discussed with some IIS engineers,
so far we may consider the following approachs if we do care about the file
size validating or controlling:

1. We can use some rich client-side scripts to validate the size of the
file (user has chosen). Here is a sample page:


=================================================
=========================
<%@ Page language="c#" Codebehind="WebForm1.aspx.cs"
AutoEventWireup="false"
Inherits="ValidateFileSizeInC.WebForm1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm1</title>
<META http-equiv="Content-Type" content="text/html; charset=windows-1252">
<meta name="GENERATOR" Content="Microsoft Visual Studio 7.0">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema"
content="http://schemas.microsoft.com/intellisense/ie5">
<script language="javascript">
function GetSize(path)
{
var fso, f1;
fso = new ActiveXObject("Scripting.FileSystemObject");
f1=fso.GetFile(path);
return f1.Size;
}

function CheckSize() {
var size=GetSize(Form1.myFile.value);
if (size > 10240) {
alert("File is too big");
window.event.returnValue = false;
}
}
</script>
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server"
enctype="multipart/form-data">
<asp:Label id="Label1" style="Z-INDEX: 101; LEFT: 29px; POSITION:
absolute; TOP:
33px" runat="server">Enter File Name:</asp:Label>
<asp:Button id="Button1" style="Z-INDEX: 102; LEFT: 35px; POSITION:
absolute;
TOP: 105px" runat="server"
Text="Upload"></asp:Button><INPUT style="Z-INDEX: 103; LEFT: 32px;
POSITION:
absolute; TOP: 64px" type="file" id="myFile"
runat="server">
</form>
</body>
</HTML>

========================

And in the code behind:

private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
Button1.Attributes.Add("onclick", "CheckSize();");
}

===================
=======================================


2. For rich and powerful file uploading, you can also consider use some
rich client components such as activex control or IE hosted .net
usercontrol to update the file from client to server. In such cases, the
error and exception handling could be done in these rich client control's
codelogic.

Hope this helps some. Thanks for your understanding.

Regards,

Steven Cheng

Microsoft MSDN Online Support Lead


==================================================

When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.

==================================================


This posting is provided "AS IS" with no warranties, and confers no rights.



Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
S

Steven Cheng[MSFT]

Hi Andrew,

How are you doing on this issue? Or does our suggestion in the former reply
helps you some? Please feel free to post here if there is anything else we
can help.

Regards,

Steven Cheng
Microsoft MSDN Online Support Lead


==================================================

When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.

==================================================


This posting is provided "AS IS" with no warranties, and confers no rights.



Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top