Recommended way to check file size on upload

前端 未结 10 792
南笙
南笙 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 10:57

    You may take a look at the File API which is a draft for HTML 5. Here's a nice article. That's what Gmail uses if the browser supports it of course. But there's no way to ensure this will work across all browsers.

    0 讨论(0)
  • 2021-02-04 10:59

    It is possible to determine the size of the file that is on uploading even before the uploaded file has been transfered completely. Even when using Internet Explorer. By using the HTTP's content-length header!
    In java world:
    First of all the if you handle the request that arrive to the server in the first servlet/filter/controller before any previous thread mass with it, you'll be albe to get the total file size in the content-length header (request.getContentLength()).

    second- have a look in an ajax implementation that prove that it is possible: http://www.ajaxfilebrowser.com/

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

    You can have the following Code to get Gmail feature:

    <html>
    <head>
    <script type="text/javascript">
    $(function()
    {
    
    $('#btnSubmit').click(function()
    {
    if (typeof (input.files) != 'undefined') {  // this will work in firefox,chrome.
       if (input.files[0].fileSize > MAX_FILE_SIZE) {
            alert("file size exceeded");
             return false;
          }
     }
    else
    {
    //this approach will work for ie 8.
    $('iframe').bind('load'),function()
    {
    try
    {
    var text = $('iframe')[0].contentWindow.document.body ? $('iframe')[0].contentWindow.document.body.innerHTML : null;
    }
    catch(e)
    {
    alert('Server error occurred');
    }
    }
    });
    });
    
    });
    </script>
    </head>
    <body>
    <form method="post" enctype="multipart/form-data" name="fileUpload" action="Default.aspx" target="responseFrame">
    <input type="file"/>
    <input type="submit" value="upload" id="btnSubmit"/>
    </form>
    <iframe src='' name='responseFrame' id='responseFrame' style='width:0px;height:0px;border:0;'></iframe>
    </body>
    </html>
    

    Hope this help.

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

    I actually asked this very question (more or less) a few days ago and the general answer seems to be: It can't be done.

    For reference: Ensure file size of uploaded file is lower than maxRequestLength before the upload occurs?

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

    I haven't tested this, but isn't there a "Content-Length" header on the request? It will include more than just the file being uploaded, but you can use it as a benchmark to determine if the POST is too large to handle.

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

    I believe file upload is based on RFC 1867, although HTML5 supersedes all this. You can perfectly reimplement this RFC, server side, and check the size of the cumulated incoming stream.

    In the most general cases, I don't think you can safely rely on the content-length as it may not represent the size of the file, but just the size of a chunk.

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