我正在读这个http://dev.w3.org/html5/markup/input.file.html http://dev.w3.org/html5/markup/input.file.html,但我只找到了“accept”属性。
我试过这个
<input type="file" name="imagefilename" accept="image/x-png, image/gif, image/jpeg" />
是否可以在客户端验证文件大小?
我发现这个技术适用于 IE
<html>
<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>
是否可以使用同样的方法html5 文件系统 API http://www.html5rocks.com/en/tutorials/file/filesystem/ ?
UPDATE
我可以这样做(demo http://jsfiddle.net/9bhcB/):
<!doctype html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.0/jquery.min.js"></script>
</head>
<body>
<form >
<input type=file id=f max-size=32154 >
<input type=submit>
</form>
<script>
$(function(){
$('form').submit(function(){
var isOk = true;
$('input[type=file][max-size]').each(function(){
if(typeof this.files[0] !== 'undefined'){
var maxSize = parseInt($(this).attr('max-size'),10),
size = this.files[0].fileSize;
isOk = maxSize > size;
return isOk;
}
});
return isOk;
});
});
</script>
</body>
它工作正常,但我认为原生 html5 attr 来验证会更好