Recommended way to check file size on upload

前端 未结 10 739
南笙
南笙 2021-02-04 10:33

I am working on a web application which supports file uploading. I am already familiar checking the size in server side, but i wanted to check the file size in a client side.

相关标签:
10条回答
  • 2021-02-04 11:13

    After lot of searching i found we can use maxAllowedContentLength to limit the upload size in ASP.Net applications. Remember this settings work only on IIS 7 or above.

    Since this is handled in IIS level, we dont need to handle anything in server code. If the size limit exceeds, it will return the error code 404.13. So you can easily check the code in client side to determine the problem.

    This is what i have done to handle the large files if the flash is not installed in the client machine.

    Hope this may help someone.

    For more info read about this settings, read this

    0 讨论(0)
  • 2021-02-04 11:19

    In IE, you can do it with JS and ActiveX:

    function A()
    {
      var oas = new ActiveXObject("Scripting.FileSystemObject");
      var d = document.a.b.value;
      var e = oas.getFile(d);
      var f = e.size;
      alert(f + " bytes");
    }
    
    </script>
    </head>
    <body>
    <form name="a">
    <input type="file" name="b">
    <input type="button" name="c" value="SIZE" onClick="A();">
    </form>
    </body>
    </html>
    
    0 讨论(0)
  • 2021-02-04 11:22

      I'll start off by saying that I haven't been able to generate a working copy and most of this is my understanding from reading around and slight expirience. That said, perhaps you could explore it a bit more and/or set me straight if I'm wrong. The main thing points to the idea of the classic form with a hidden input named MAX_FILE_SIZE... In PHP/Apache, the server will stop if the upload is larger than the MAX_FILE_SIZE (see last post from here). On the PHP side of things, the $_FILES array will return an error code (which can be seen here). Take note of UPLOAD_ERR_FORM_SIZE.

       To put this all together, you could have JavaScript upload the file within an IFrame and get the result. If the server spits out an error message (which, in this theory, would be quite quickly), JavaScript can simply alert the user. Otherwise, we can assume the file is now uploaded successfully. Now trying to make sense of Google's code is near impossible; a quick HTML scan is as far as I go and it didn't help any... They do indeed have hidden input fields but none with MAX_FILE_SIZE as the name. They are much shorter and most don't appear to have values. But, I believe this could be possible. Thoughts anyone?

      Who knows, perhaps the great Google Web Server is built in with the power to cut off uploads instantly. Thanks for listening!

    0 讨论(0)
  • 2021-02-04 11:24

    lol ofc you can do it, it's just a bit of hardwork, if you search for "uploadbar flash" instead of using the data you retrieve to create a uploader you could use it to pass it on or compare or do whatever you want the one i found that seemed quite good is this one, it doesnt necceraly use flash but you could use it to achive the same thing http://www.plupload.com highly configurable p.s. you could force it to just use flash if you really like

    0 讨论(0)
提交回复
热议问题