title: Tips. ASP.NET C# : Check upload file size
body: <p>When client trying to file upload, server need file size limit check.<br/>
  Actually the best way is .. Check on the client side I mean on web browser
  (Safari ,Chrome, IE ... etc) then Blocking Upload files if file size is over 
  , before uploaded file on server .</p>
  
  <p>But here is problem . We can check filesize on Chrome or Firefox Before 
  uploaded file to server like this below code</p>
  
  <pre><code>$("#AttachFile").change(function () {       
       var iSize = 0;
  
       if($.browser.msie){     
            $("#AttachFile").on( $.browser.msie? ‘propertychange': ‘change’, function(){
  
                 if(f.size.toFixed(0) &gt; 2000){
                      alert("File size over");
                           clear_file();
                      }
              });
  }
  </code></pre>
  
  <p>It doesn't  works in IE ! so we never know file size before uploaded 
  in server on IE browser But .. here is another problem, It's a specify of 
  <code>maxRequestLength</code> element of <code>HttpRuntime</code> TAG in <code>Web.config</code></p>
  
  <pre><code>&lt;httpRuntime maxRequestLength = "number" &gt;
  </code></pre>
  
  <p>This Elements Checks upload file-size limit and Defaults value is approximately 
  4 Megabyes .. and It can't handle on the Handles .. like . alerting to client.
  Go to Exception , even if we add file size check code on Server.
  So the answer is simple , you should specified explicitly
   <code>maxRequestLength</code> high bandwidth </p>
  
  <pre><code>&lt;httpRuntime maxRequestLength = "716800" &gt;
  </code></pre>
  
  <p>like this. you can't still handle over 716800 bytes . but you can check widely
  Below code is example of file-size check on server</p>
  
  <pre><code>if (AttachFile.PostedFile.ContentLength &gt; 4096000)
  {
          ClientScript.RegisterStartupScript(this.GetType(), "attach"
                  , "&lt;script language='javascript'&gt;\nalert('File size over.');\n&lt;/script&gt;");
  
          //Remove Uploaded files code here
  }
  </code></pre>