Click here to Skip to main content
14,767,355 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I know the default upload max is 4MB, if we upload some file larger than this, the ugly error page will pop up.
I find somewhere the solution is enlarge the maxRequestLength is web.config. But I think it's not done, the users can always upload the file larger than the maxRequestLength, I want to know how can I pop up a message box when the upload length is exceed the limit, thanks!

In my website, I write some code in global.asax file, but it didn't work:
void Application_Error(object sender, EventArgs e)
    // Code that runs when an unhandled error occurs
    HttpException h = Server.GetLastError() as HttpException;
    if (h != null && h.WebEventCode == System.Web.Management.WebEventCodes.RuntimeErrorPostTooLarge)
                        Page page = HttpContext.Current.CurrentHandler as Page;
            if (page is Page)
                page.ClientScript.RegisterStartupScript(this.GetType(), "Error", "<script>alert('error');< / script>");
        catch (Exception)



In html5 you can check the file size :[^]
based on error code you can redirect users to user friendly error page or you can
method and check the size. check both answers on below link[^]
Hi Johnson,

You can set Limit in your Web Config

    <httpRuntime maxRequestLength="x" />

where x is your max file size in KB.

Good Luck
Have a look on below links:
Link 1
Link 2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900