Is there any way to alter the error that occurs when a user uploads a file that exceeds the max attachment size?

Question:
I was wondering if there is any way to alter the error that occurs when a user uploads a file that exceeds the max attachment size. The error that currently displays is not user friendly (Maximum request length exceeded). I would like to update it to something like “The file you are attempting to upload exceeds the maximum attachment size allowed (xxMb).”

Solution:
This error causes an HttpException (HTTP Code 500). When this exception is thrown, IIS kills the connection immediately. It can be handled in the default.aspx.cs page of the form, but it is not recommended to modify this file because it will get overwriten each time you make a small modification to the form.

A good alternative would be to handle this in the backend code (.NET section in the form builder):

You can retrieve the maxRequestLength with the following code:

System.Web.Configuration.HttpRuntimeSection runTime = (System.Web.Configuration.HttpRuntimeSection)System.Configuration.ConfigurationManager.GetSection("system.web/httpRuntime");
double maxRequestLength = (runTime.MaxRequestLength - 100) * 1024;

The following steps are:

  1. Create a custom submit event.
  2. Get the file length and compare it to the maxRequestLength limit.
  3. If the length exceeds the maximum length, display an error message and cancel the submission of the form.
  4. Otherwise submit the form.