locked
how to check uploaded filesize .i want uploaded video size less than 20mb. thanks in advance RRS feed

  • Question

  • User1493762548 posted

    the following code did not checked size .please help to check the size .it should be less than 20mb.

    var FileName = "";
        if (IsPost) {
            var fileSavePath = "";
            var uploadedFile = Request.Files[0];
           var fileSize=uploadedFile.ContentLength;
            if (fileSize>2000){
                ModelState.AddFormError("the file size must be less than 15 MB(15 second 320* 480 or 10 second HDvideo) ");
            }
            FileName = Path.GetFileName(uploadedFile.FileName);
             var FileExtension = Path.GetExtension(uploadedFile.FileName).Trim();
             if(uploadedFile!= null && FileExtension==".mp4"){
               
    Wednesday, June 22, 2016 5:06 AM

Answers

  • User-471420332 posted

    Hi fiazahmed,

    The default maximum, imposed by ASP.NET, is 4MB. However you can change that by setting the maxRequestLength in the web config.

    https://msdn.microsoft.com/en-us/library/e1f13641(VS.80).aspx

    <configuration>
      <system.web>
        <httpRuntime maxRequestLength="20480" />
      </system.web>
    </configuration>
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, June 22, 2016 5:28 AM
  • User-821857111 posted

    You might be better doing this check on the client, initially.

    <!DOCTYPE html>
    
    <html lang="en">
        <head>
            <meta charset="utf-8" />
            <title></title>
            <script>
                window.onload = function () {
                    var fileInput = document.getElementById('fileInput');
                    fileInput.addEventListener('change', function (e) {
                        if (typeof FileReader !== "undefined") {
                            var size = fileInput.files[0].size;
                            if(size > 20000000){
                                alert('Too big');    
                            }
                        }
                    })
                }
            </script>
        </head>
        <body>
            <input type="file" id="fileInput">
        </body>
    </html>

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, June 28, 2016 8:36 AM

All replies

  • User-471420332 posted

    Hi fiazahmed,

    The default maximum, imposed by ASP.NET, is 4MB. However you can change that by setting the maxRequestLength in the web config.

    https://msdn.microsoft.com/en-us/library/e1f13641(VS.80).aspx

    <configuration>
      <system.web>
        <httpRuntime maxRequestLength="20480" />
      </system.web>
    </configuration>
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, June 22, 2016 5:28 AM
  • User-166373564 posted

    Hi fiazahmed,

    For IIS 7.5 and above, you need also add the httpRuntime maxRequestLength setting:

      <system.webServer>
        <security>
          <requestFiltering>
            <requestLimits maxAllowedContentLength="52428800" /> <!--50MB-->
          </requestFiltering>
        </security>
      </system.webServer>
    Saturday, June 25, 2016 3:54 AM
  • User-821857111 posted

    You might be better doing this check on the client, initially.

    <!DOCTYPE html>
    
    <html lang="en">
        <head>
            <meta charset="utf-8" />
            <title></title>
            <script>
                window.onload = function () {
                    var fileInput = document.getElementById('fileInput');
                    fileInput.addEventListener('change', function (e) {
                        if (typeof FileReader !== "undefined") {
                            var size = fileInput.files[0].size;
                            if(size > 20000000){
                                alert('Too big');    
                            }
                        }
                    })
                }
            </script>
        </head>
        <body>
            <input type="file" id="fileInput">
        </body>
    </html>

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, June 28, 2016 8:36 AM