Bre*_*ton 13
从编辑开始更新2013年,所有主流浏览器都支持File API,从版本10开始支持IE
http://caniuse.com/#search=file%20api
如果你仍然需要支持IE9及更低版本,你可能仍然希望使用SWFUpload,尽管此时它应该更多的是后备,因为html5文件api在SWFUpload无法访问的移动平台上提供支持.html5文件api基于firefox的文件api,如下所示.
另请参阅此重复问题 客户端使用HTML5检查文件大小?
更新: Firefox已将其API更改为此https://developer.mozilla.org/en/DOM/FileReader
你可以在firefox中这样做
HTML:
Run Code Online (Sandbox Code Playgroud)<form action="" method="get" accept-charset="utf-8"> <input type="file" name="file" value="" id="file"> <p><input type="submit" value="Continue →"></p> </form>
JavaScript的:
var filesize = document.forms[0].file.files[0].fileSize
Run Code Online (Sandbox Code Playgroud)
如果在IE中有办法做到这一点,我不知道.它可能涉及activeX或其他一些垃圾.
编辑:我在这里发现了这个,如何在IE浏览器中这样做
<head>
<script>
function getSize()
{
var myFSO = new ActiveXObject("Scripting.FileSystemObject");
var filepath = document.upload.file.value;
var thefile = myFSO.getFile(filepath);
var size = thefile.size;
alert(size + " bytes");
}
</script>
</head>
<body>
<form name="upload">
<input type="file" name="file">
<input type="button" value="Size?" onClick="getSize();">
</form>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)