How to check file input size with jQuery?

前端 未结 8 2321
渐次进展
渐次进展 2020-11-22 09:21

I have a form with file upload capabilities and I would like to be able to have some nice client side error reporting if the file the user is trying to upload is too big, is

相关标签:
8条回答
  • 2020-11-22 09:29

    If you want to use jQuery's validate you can by creating this method:

    $.validator.addMethod('filesize', function(value, element, param) {
        // param = size (en bytes) 
        // element = element to validate (<input>)
        // value = value of the element (file name)
        return this.optional(element) || (element.files[0].size <= param) 
    });
    

    You would use it:

    $('#formid').validate({
        rules: { inputimage: { required: true, accept: "png|jpe?g|gif", filesize: 1048576  }},
        messages: { inputimage: "File must be JPG, GIF or PNG, less than 1MB" }
    });
    
    0 讨论(0)
  • 2020-11-22 09:33

    You actually don't have access to filesystem (for example reading and writing local files), however, due to HTML5 File Api specification, there are some file properties that you do have access to, and the file size is one of them.

    For the HTML below

    <input type="file" id="myFile" />
    

    try the following:

    //binds to onchange event of your input field
    $('#myFile').bind('change', function() {
    
      //this.files[0].size gets the size of your file.
      alert(this.files[0].size);
    
    });
    

    As it is a part of the HTML5 specification, it will only work for modern browsers (v10 required for IE) and I added here more details and links about other file information you should know: http://felipe.sabino.me/javascript/2012/01/30/javascipt-checking-the-file-size/


    Old browsers support

    Be aware that old browsers will return a null value for the previous this.files call, so accessing this.files[0] will raise an exception and you should check for File API support before using it

    0 讨论(0)
  • 2020-11-22 09:33

    Use below to check file's size and clear if it's greater,

        $("input[type='file']").on("change", function () {
         if(this.files[0].size > 2000000) {
           alert("Please upload file less than 2MB. Thanks!!");
           $(this).val('');
         }
        });
    
    0 讨论(0)
  • 2020-11-22 09:35

    You can do this type of checking with Flash or Silverlight but not Javascript. The javascript sandbox does not allow access to the file system. The size check would need to be done server side after it has been uploaded.

    If you want to go the Silverlight/Flash route, you could check that if they are not installed to default to a regular file upload handler that uses the normal controls. This way, if the do have Silverlight/Flash installed their experience will be a bit more rich.

    0 讨论(0)
  • 2020-11-22 09:45

    Plese try this:

    var sizeInKB = input.files[0].size/1024; //Normally files are in bytes but for KB divide by 1024 and so on
    var sizeLimit= 30;
    
    if (sizeInKB >= sizeLimit) {
        alert("Max file size 30KB");
        return false;
    }
    
    0 讨论(0)
  • 2020-11-22 09:47

    This code:

    $("#yourFileInput")[0].files[0].size;
    

    Returns the file size for an form input.

    On FF 3.6 and later this code should be:

    $("#yourFileInput")[0].files[0].fileSize;
    
    0 讨论(0)
提交回复
热议问题