Click here to Skip to main content
15,881,870 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I’m trying to limit the file upload size of a site to 2 MB. For this, I tried two methods, both of which failed.

In the first method, I set the maxRequestLength attribute of the httpRuntime element in the web.config file to 2048 KB. During the upload in the corresponding page, I check the size of the uploading file and allow upload if it’s within the permissible limit.

Example:

JavaScript
<configuration>
  <system.web>
    <compilation debug="true" strict="false" explicit="true" targetFramework="4.0" />
    <pages styleSheetTheme="Default"></pages>
    <httpRuntime maxRequestLength = "2048" />
  </system.web>
  <connectionStrings>
    <add name="TestConnection" connectionString="Data Source=localhost;Initial Catalog=TestDB;User ID=sa;password=" />
  </connectionStrings>
</configuration>


In the code editor of the page:

VB
'Check if the size of the file is within 2 MB (2097152 in bytes).
If ImageUpload.PostedFile.ContentLength <= 2097152 Then
	'Code to upload…
End If


If the file size is within 2 MB then everything goes fine. As soon as the file size exceeds the limit, it crashes. As a workaround, I increased maxRequestLength in web.config to a much higher value- 35840 KB (35 MB) while keeping the validation method in the page intact. This time, any file with actual memory of 35 MB or less is validated properly with a constraint of 2 MB by the page method. But as soon the actual memory of the file exceeds 35 MB the site crashes again.

In the second method, I added a Global.asax file to the site. In its Application_Error method I check if the exception is raised due to exceeding of the maximum request length. If yes, then it should redirect to the same page from which the exception is raised with an error message in the query string. When the page re-loads it should show the error message.
But during runtime, though the Application_Error method is getting executed, it crashes again.

Global.asax example:

VB
Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
        ' Code that runs when an unhandled error occurs.

        ' Handle errors that occur while uploading files larger than permissible 2 MB limit.
        If HttpContext.Current.Request.Url.ToString.Contains("Customer.aspx") Then
            If HttpContext.Current.Error.InnerException.Message.Contains("Maximum request length exceeded") Then
                HttpContext.Current.ClearError()
                HttpContext.Current.Response.Redirect(String.Format("~/Customer.aspx?ErrorMessage={0}", "The file cannot be uploaded because it exceeds the 2 MB size limit."))
            End If
        End If
    End Sub


Please help to sort out this problem. Regards.
Posted
Comments
ZurdoDev 13-Aug-13 12:59pm    
What do you mean crashes? Do you get an error stating that the file is too big?
cigwork 13-Aug-13 15:11pm    
You can't validate filesize server side this way.

If I remember correctly the HTTP max request is intended to be used as a first line of defence against DOS attacks mounted using large requests. For this reason any request exceeding the server's configured limit will throw an exception before any application code is executed.
priyamtheone 29-Aug-13 10:50am    
Thanks. Please refer to my newest post for further details.
priyamtheone 4-Sep-13 11:16am    
Sorry my last post didn't get submitted. I updated it once again. Please check the comments section of the post of @db7uk.
Abdullah Sarfraz 12-Aug-23 11:24am    
Hi. I increased file upload size & execution time then my website even my wordpress dashboard crash with internal server error. please help. actually, I downloaded the actual .htaccess file, edited it, deleted the original .htaccess and pasted it in public html

What is the error you are getting? Is the App pool crashing? Is the server crashing? Is there an error message?

Have you looked at executionTimeout as it maybe a timeout issue not size issue?

XML
<configuration>
  <system. web=""> 
    <httpruntime maxrequestlength="2048" executiontimeout="3600" />
  br mode="hold" /></system.></configuration>


Have you also looked at setting the server maxAllowedContentLength (IIS7) ?

XML
<system.webserver>
  <security>
    <requestfiltering>
       <requestlimits maxallowedcontentlength="Big number here" />
    </requestfiltering>
 </security>
</system.webserver>


What about these links?

http://blogs.msdn.com/b/prashant_upadhyay/archive/2011/07/13/large-file-upload-issue-in-asp-net.aspx[^]

http://www.webdavsystem.com/server/documentation/large_files_iis_asp_net[^]
 
Share this answer
 
Comments
priyamtheone 29-Aug-13 10:49am    
Thanks, but the way you described didn't work.
Somehow my last post didn't get submitted. So I'm re-posting them as multiple comments here. Please take a little time in focusing on them.
priyamtheone 4-Sep-13 11:08am    
I tried using executionTimeout, requestLengthDiskThreshold and maxAllowedContentLength.

<configuration>
<system.web>
<compilation debug="true" strict="false" explicit="true" targetFramework="4.0" />
<pages styleSheetTheme="Default"></pages>
<httpRuntime maxRequestLength = "2048" executionTimeout="3600" requestLengthDiskThreshold="2048" />
</system.web>
<connectionStrings>
<add name="TestConnection" connectionString="Data Source=localhost;Initial Catalog=TestDB;User ID=sa;password=" />
</connectionStrings>
<system.webServer>
<security>
<requestFiltering>
<requestLimits maxAllowedContentLength="20480000"></requestLimits>
</requestFiltering>
</security>
</system.webServer>
</configuration>

But the same error occurs. Please find the screenshots of the error in different browsers in the links below:

Chrome 26.0.1410.43
https://docs.google.com/file/d/0B3aGOxBwxg17RDN3blQ5T1FTOWc/edit?usp=sharing[^]

Firefox 14.0.1
https://docs.google.com/file/d/0B3aGOxBwxg17VlIyUXptaUliQ3c/edit?usp=sharing[^]

Internet Explorer 8
https://docs.google.com/file/d/0B3aGOxBwxg17Ri1CYVJHS3IxUDg/edit?usp=sharing[^]

Safari 5.1.7
https://docs.google.com/file/d/0B3aGOxBwxg17U1UzZFZOc3d3SFU/edit?usp=sharing[^]

From the comment of @cigwork and http://blogs.msdn.com/b/prashant_upadhyay/archive/2011/07/13/large-file-upload-issue-in-asp-net.aspx, it seems it cannot be done the way I want. We’ll have to go for a workaround. The one that I can think of is, set maxRequestLength to a big value e.g. 104857600 (100 GB) in web.config and validate uploading file size for 2 MB in the respective page separately.

Please share if anybody comes up with a better idea.
Thanks!
I tried using executionTimeout, requestLengthDiskThreshold and maxAllowedContentLength.

XML
<configuration>
  <system.web>
    <compilation debug="true" strict="false" explicit="true" targetFramework="4.0" />
    <pages styleSheetTheme="Default"></pages>
    <httpRuntime maxRequestLength = "2048" executionTimeout="3600" requestLengthDiskThreshold="2048" />
  </system.web>
  <connectionStrings>
    <add name="TestConnection" connectionString="Data Source=localhost;Initial Catalog=TestDB;User ID=sa;password=" />
  </connectionStrings>
  <system.webServer>
    <security>
      <requestFiltering>
        <requestLimits maxAllowedContentLength="20480000"></requestLimits>
      </requestFiltering>
    </security>
  </system.webServer>
</configuration>


But the same error occurs. Please find the screenshots of the error in different browsers in the links below:

Chrome 26.0.1410.43
https://docs.google.com/file/d/0B3aGOxBwxg17RDN3blQ5T1FTOWc/edit?usp=sharing[^]

Firefox 14.0.1
https://docs.google.com/file/d/0B3aGOxBwxg17VlIyUXptaUliQ3c/edit?usp=sharing[^]

Internet Explorer 8
https://docs.google.com/file/d/0B3aGOxBwxg17Ri1CYVJHS3IxUDg/edit?usp=sharing[^]

Safari 5.1.7
https://docs.google.com/file/d/0B3aGOxBwxg17U1UzZFZOc3d3SFU/edit?usp=sharing[^]

From the comment of @cigwork and http://blogs.msdn.com/b/prashant_upadhyay/archive/2011/07/13/large-file-upload-issue-in-asp-net.aspx, it seems it cannot be done the way I want. We’ll have to go for a workaround. The one that I can think of is, set maxRequestLength to a big value e.g. 104857600 (100 GB) in web.config and validate uploading file size for 2 MB in the respective page separately.

Please share if anybody comes up with a better idea.
Thanks!
 
Share this answer
 

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