我可以在文件类型输入中显示文件大小吗?

Can I display a file size in a file type input?

本文关键字:显示 文件大小 输入 文件 类型 我可以      更新时间:2023-09-26

我正在使用文件类型输入。我想在文件被选中后显示文件大小。我认为我应该使用一些jquery拉,但我不确定如何。我可以这么做吗?

<input type="file" id="uploadFile" name="uploadFile"/>

如果浏览器有以下功能(firefox, chrome, opera(?)):

jQuery( "#uploadFile").bind( "change", function(){
    if( this.files && this.files.length ) { //If this condition passes, you can get file size
    var file = this.files[0],
    fileSize = file.size || file.fileSize || 0;
    alert(fileSize+ " bytes" );
    }
});

demo在这里:http://jsfiddle.net/yNQ9M/1/(更新为在firefox和opera中工作)

你也可以使用ActionScript (Flash) hack在旧的浏览器中做到这一点。

微软正在开发ie10的File API