0

I am using ASP:FileUpload Control for uploading the PDF files. I have limited the upload file size to 1MB in Web.Config like this.

    <system.web>
    <httpRuntime  maxRequestLength="1024" executionTimeout="360"/>
    </system.web>

ASPX code

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs"   Inherits="WebpageUpload.WebForm1" %>

    <%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="Ajax" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0  Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head id="Head1" runat="server">
    <title></title>
    </head>
    <body>
    <form id="form1" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
    <div>
    Normal File Upload...
    <asp:FileUpload ID="FileUpload1" runat="server" />
    <asp:Button ID="Button1" runat="server" Text="Clear" OnClick="Button1_Click" />
    </div>
    </form>
    </body>
    </html>

Here the main issue is, When i upload a file more than 1 MB and click on Clear button, I am getting "Internet Explorel Cannot display this webpage."

Please find the Image for reference..

Thanks in advance...

karthik k
  • 3,751
  • 15
  • 54
  • 68

2 Answers2

0

When you set that value in web.config you tell IIS that you are going to be exceptioning on that condition. That means a YSOD. You need to either catch that exception and handle it gracefully or raise the limit and check it by hand. Unfortunately, with the file upload control, there is no way to check the file size ahead of time before the upload is initiated.

Fourth
  • 9,163
  • 1
  • 23
  • 28
  • Hi..Thanks for the reply. How can i catch that exception. Is there any config changes need to be done. Could you please any code snippet or any suggession onthis how to achieve? – karthik k May 23 '11 at 13:57
  • It's not trivial, but see this thread: http://stackoverflow.com/questions/665453/catching-maximum-request-length-exceeded – Fourth May 23 '11 at 14:07
0

If you want to restrict the user to upload files to less than 1 MB, you have to put in the following condition and check the file size before uploading.

if (FileUpload2.FileContent.Length > 1048576)
    {
         //File Size is greater than 1 MB
    }
Off The Gold
  • 1,228
  • 15
  • 28
Muhammad Akhtar
  • 51,913
  • 37
  • 138
  • 191
  • The problem with this is that IIS doesnt know the file size until it hits the limit. I have tried several times, unsuccessfully, and the file size isn't part of the request so you cant know how big it is until the upload is either complete or until you hit the limit. – Fourth May 23 '11 at 13:59