Closed. This question needs debugging details。它当前不接受答案。












想要改善这个问题吗?更新问题,以便将其作为on-topic用于堆栈溢出。

5年前关闭。



Improve this question




如果值> 1024,我想获取文件大小,以MB为单位;如果
$(document).ready(function() {
    $('input[type="file"]').change(function(event) {
        var _size = this.files[0].size + 'mb';
        alert(_size);
    });
});

<input type="file">

最佳答案

请在下面找到更新的代码。参见工作示例here,干杯!

 $(document).ready(function() {
        $('input[type="file"]').change(function(event) {
            var _size = this.files[0].size;
            var fSExt = new Array('Bytes', 'KB', 'MB', 'GB'),
        	i=0;while(_size>900){_size/=1024;i++;}
            var exactSize = (Math.round(_size*100)/100)+' '+fSExt[i];
                console.log('FILE SIZE = ',exactSize);
        	alert(exactSize);
        });
    });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<input type="file" />

09-11 14:12