checking image size before upload

后端 未结 6 2031
暖寄归人
暖寄归人 2020-12-11 08:48

I noticed when uploading a profile pic to Twitter that its size is checked before upload. Can anyone point me to a solution like this?

Thanks

6条回答
  •  情书的邮戳
    2020-12-11 09:40

    This works perfectly for me

    var _URL = window.URL || window.webkitURL;
    
    $("#file").change(function(e) {
    var file, img;
    
    
    if ((file = this.files[0])) {
        img = new Image();
        img.onload = function() {
            if((this.width < 800)||(this.height < 400)){
                alert('Image too small. Images must be bigger than 800 x 400');
                $('#file').val('');
            }
    
        };
        img.onerror = function() {
            alert( "not a valid file: " + file.type);
        };
        img.src = _URL.createObjectURL(file);
    
    }
    
    });
    

    Kudos to the original creator: http://jsfiddle.net/4N6D9/1/

    I modified this to specify the min width and height of an upload.

提交回复
热议问题