日期:2014-05-16 浏览次数:20461 次
在上传文件文件之前,有时候需要验证文件的大小,先提供如下方法,获取文件浏览框的大小,代码如下(适合ie,firefox,google chrome)
?
?
//obj 需要传入的参数为Input的对象
function getFileSize(obj){
var objValue = obj.value;
if (objValue=="") return ;
var fileLenth=-1;
try {
//对于IE判断要上传的文件的大小
var fso = new ActiveXObject("Scripting.FileSystemObject");
fileLenth=parseInt(fso.getFile(objValue).size);
} catch (e){
try{
//对于非IE获得要上传文件的大小
fileLenth=parseInt(obj.files[0].size);
}catch (e) {
fileLenth=-1;
}
}
return fileLenth;
}