prevent Upload Large Files .Net

Discussion in 'ASP .Net Web Controls' started by Kilic Beg, Nov 24, 2004.

  1. Kilic Beg

    Kilic Beg Guest

    Hi,

    I want to prevent user to upload files larger than 2MB.
    If they try to upload I want to redirect them to a error page (ex: File size
    larger than 2 MB )..
    right now I get a page break "The page cannot be displayed"

    I tried to update the Global.asax.cs file this is the code

    protected void Application_BeginRequest(Object sender, EventArgs e)
    {
    long MAX_CONTENTLENGTH = 2097152;
    if (Request.ContentLength > MAX_CONTENTLENGTH) {
    //Response.Write("File is to big");
    Response.Redirecty("Error.html");
    }
    }

    I put the same code also into
    protected void Application_Error(Object sender, EventArgs e)
    method..

    still page break..

    where and how can I catch that and redirect to a friendly error page.
    do I have to use a third party product for that ?

    Please help !!!

    Thanks

    - Kilic
    Kilic Beg, Nov 24, 2004
    #1
    1. Advertising

  2. Kilic Beg

    Teemu Keiski Guest

    Hi,

    you can also set this on web.config. see <httpRuntime> element and
    especially maxRequestLength setting
    http://msdn.microsoft.com/library/d...-us/cpgenref/html/gngrfhttpruntimesection.asp

    Just note that this effects entire application at once (if it is config file
    on app root, on subfolder you can limit it to folder/page level)

    --
    Teemu Keiski
    MCP, Microsoft MVP (ASP.NET), AspInsider
    ASP.NET Forum Moderator, AspAlliance Columnist
    http://blogs.aspadvice.com/joteke


    "Kilic Beg" <> wrote in message
    news:...
    > Hi,
    >
    > I want to prevent user to upload files larger than 2MB.
    > If they try to upload I want to redirect them to a error page (ex: File
    > size
    > larger than 2 MB )..
    > right now I get a page break "The page cannot be displayed"
    >
    > I tried to update the Global.asax.cs file this is the code
    >
    > protected void Application_BeginRequest(Object sender, EventArgs e)
    > {
    > long MAX_CONTENTLENGTH = 2097152;
    > if (Request.ContentLength > MAX_CONTENTLENGTH) {
    > //Response.Write("File is to big");
    > Response.Redirecty("Error.html");
    > }
    > }
    >
    > I put the same code also into
    > protected void Application_Error(Object sender, EventArgs e)
    > method..
    >
    > still page break..
    >
    > where and how can I catch that and redirect to a friendly error page.
    > do I have to use a third party product for that ?
    >
    > Please help !!!
    >
    > Thanks
    >
    > - Kilic
    >
    >
    >
    Teemu Keiski, Nov 24, 2004
    #2
    1. Advertising

  3. Kilic Beg

    Kilic Beg Guest

    My problem is not setting the file size limit.

    I want to display an error message when the users tries to upload a file
    large then in maxRequestLength definied.

    right now I have this in my webconfig

    <httpRuntime executionTimeout="90" maxRequestLength="4096"
    useFullyQualifiedRedirectUrl="false" minFreeThreads="8"
    minLocalRequestFreeThreads="4" appRequestQueueLimit="100" />

    If the users tries to upload files larger than 4MB he will get a page break
    "The page cannot be displayed"..
    I want to display a friendly page... (ex: saying the file you trying to
    uploading is larger than 4MB)

    - Kilic



    "Teemu Keiski" <> wrote in message
    news:...
    > Hi,
    >
    > you can also set this on web.config. see <httpRuntime> element and
    > especially maxRequestLength setting
    >

    http://msdn.microsoft.com/library/d...-us/cpgenref/html/gngrfhttpruntimesection.asp
    >
    > Just note that this effects entire application at once (if it is config

    file
    > on app root, on subfolder you can limit it to folder/page level)
    >
    > --
    > Teemu Keiski
    > MCP, Microsoft MVP (ASP.NET), AspInsider
    > ASP.NET Forum Moderator, AspAlliance Columnist
    > http://blogs.aspadvice.com/joteke
    >
    >
    > "Kilic Beg" <> wrote in message
    > news:...
    > > Hi,
    > >
    > > I want to prevent user to upload files larger than 2MB.
    > > If they try to upload I want to redirect them to a error page (ex: File
    > > size
    > > larger than 2 MB )..
    > > right now I get a page break "The page cannot be displayed"
    > >
    > > I tried to update the Global.asax.cs file this is the code
    > >
    > > protected void Application_BeginRequest(Object sender, EventArgs e)
    > > {
    > > long MAX_CONTENTLENGTH = 2097152;
    > > if (Request.ContentLength > MAX_CONTENTLENGTH) {
    > > //Response.Write("File is to big");
    > > Response.Redirecty("Error.html");
    > > }
    > > }
    > >
    > > I put the same code also into
    > > protected void Application_Error(Object sender, EventArgs e)
    > > method..
    > >
    > > still page break..
    > >
    > > where and how can I catch that and redirect to a friendly error page.
    > > do I have to use a third party product for that ?
    > >
    > > Please help !!!
    > >
    > > Thanks
    > >
    > > - Kilic
    > >
    > >
    > >

    >
    >
    Kilic Beg, Nov 24, 2004
    #3
  4. On Tue, 23 Nov 2004 21:02:51 -0500 in
    microsoft.public.dotnet.framework.aspnet.webcontrols, "Kilic Beg"
    <> wrote:

    >Hi,
    >
    >I want to prevent user to upload files larger than 2MB.
    >If they try to upload I want to redirect them to a error page (ex: File size
    >larger than 2 MB )..
    >right now I get a page break "The page cannot be displayed"
    >


    Kilic,

    Not sure how to check the request size, but if you're using one of the
    ..NET file upload controls (System.Web.UI.HtmlControls.HtmlInputFile or
    2.0 FileUpload) you could simply write

    System.Web.UI.HtmlControls.HtmlInputFile File1;

    if (File1.PostedFile.ContentLength > MAXFILESIZE) {
    Response.Redirect("filetoolarge.aspx");
    }
    else {
    // Save the file on the server
    }
    Roger Helliwell, Nov 25, 2004
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Raj Tripathi

    Upload large files to server problem

    Raj Tripathi, Nov 20, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    403
    Alex Papadimoulis
    Nov 20, 2003
  2. Charlie@CBFC
    Replies:
    1
    Views:
    528
    Joshua Flanagan
    May 3, 2005
  3. =?Utf-8?B?U2FuZHk=?=

    upload very large files

    =?Utf-8?B?U2FuZHk=?=, Nov 2, 2006, in forum: ASP .Net
    Replies:
    2
    Views:
    399
    =?Utf-8?B?U2FuZHk=?=
    Nov 3, 2006
  4. Bobby Edward

    Control to upload LARGE files

    Bobby Edward, Nov 12, 2008, in forum: ASP .Net
    Replies:
    7
    Views:
    528
    Bobby Edward
    Nov 13, 2008
  5. upload large files not working

    , Dec 1, 2005, in forum: ASP General
    Replies:
    10
    Views:
    275
    Kyle Peterson
    Dec 4, 2005
Loading...

Share This Page