Friday, 21 December 2012

Set Maximum file upload size and get the value in C#


Add these function in Web Config file
--------------------------------------
Normal file upload size is 4MB. Here Under system.web maxRequestLength mentioned in KB and in system.webServer maxAllowedContentLength as in Bytes.

<system.web>
  .
  .
  .
  <httpRuntime executionTimeout="3600" maxRequestLength="102400" useFullyQualifiedRedirectUrl="false" delayNotificationTimeout="60"/>
</system.web>


<system.webServer>
  .
  .
  .
  <security>
      <requestFiltering>
        <requestLimits maxAllowedContentLength="1024000000" />
        <fileExtensions allowUnlisted="true"></fileExtensions>
      </requestFiltering>
    </security>
</system.webServer>


and if we want to know the maxFile upload size use the given line in .cs page

System.Configuration.Configuration config = WebConfigurationManager.OpenWebConfiguration("~");
HttpRuntimeSection section = config.GetSection("system.web/httpRuntime") as HttpRuntimeSection;
               
double maxFileSize = Math.Round(section.MaxRequestLength / 1024.0, 1);

No comments:

Post a Comment