可通过下面的方式
html代码: js代码:
var fileId = "uploadtest"
var dom = document.getElementById(fileId)
var fileSize = dom.files[0].size//文件的大小,单位为字节B
可以通过.size属性获取,但是对浏览器兼容性有要求,不兼容IE9以下版本。放个demo:
<!DOCTYPE html><html>
<head>
<script>
function upload() {
var file = document.getElementById("file").files[0]
alert(file.size)
}
</script>
</head>
<body>
<input type="file" id="file" />
<button id="upload" onclick="upload()">上传</button>
</body>
</html>
获取上传文件的大小:<input type="file" name="fileId" id="fileId" class="height30 width-245">
function findSize(field_id){
var byteSize = 0
if (!+[1,] &&!$("#"+field_id).files) {
var filePath = $("#"+field_id).val()
var fileSystem = new ActiveXObject("Scripting.FileSystemObject")
byteSize = fileSystem.GetFile(filePath).size
} else{
var fileInput = $("#"+field_id)[0]
byteSize = fileInput.files[0].size
}
return ( Math.ceil(byteSize / 1024) )
}