原文地址: http://www.dotblogs.com.tw/cross/archive/2010/09/21/17840.aspx
google了很久,基本上都是用 activeX 来实现~至于为什么不行,这个不多说,说一下以下方法:
已经测试通过的浏览器:IE6+,firefox,chrome,其中 firefox 和 chrome 要能支持 HTML5。
<
html
>
< head >
< meta http-equiv ="Content-Type" content ="text/html; charset=big5" >
< title >上传 </ title >
</ head >
< body >
< form action ="XXXXXX" method ="POST" name ="FileForm" enctype ="multipart/form-data" >
< div align ="center" >
图片:
< input type ="file" name ="file1" size ="20" id ="file1" />
< input type ="button" onclick ="checkFile()" /></ div >
</ form >
</ body >
</ html >
< head >
< meta http-equiv ="Content-Type" content ="text/html; charset=big5" >
< title >上传 </ title >
</ head >
< body >
< form action ="XXXXXX" method ="POST" name ="FileForm" enctype ="multipart/form-data" >
< div align ="center" >
图片:
< input type ="file" name ="file1" size ="20" id ="file1" />
< input type ="button" onclick ="checkFile()" /></ div >
</ form >
</ body >
</ html >
<script src="http://code.jquery.com/jquery-1.4.2.min.js" type="text/javascript"></script>
<script language="JavaScript" type="text/javascript">
var fileSize = 0; // 文件大小
var SizeLimit = 1024; // 上传上限,单位:byte
function checkFile() {
var f = document.getElementById("file1");
// FOR IE
if ($.browser.msie) {
var img = new Image();
img.onload = checkSize;
img.src = f.value;
}
// FOR Firefox,Chrome
else {
fileSize = f.files.item(0).size;
checkSize();
}
}
// 检查文件大小
function checkSize() {
// FOR IE FIX
if ($.browser.msie) {
fileSize = this.fileSize;
}
if (fileSize > SizeLimit) {
alert('文件超过大小');
} else {
document.FileForm.submit();
}
}
</script>
<script language="JavaScript" type="text/javascript">
var fileSize = 0; // 文件大小
var SizeLimit = 1024; // 上传上限,单位:byte
function checkFile() {
var f = document.getElementById("file1");
// FOR IE
if ($.browser.msie) {
var img = new Image();
img.onload = checkSize;
img.src = f.value;
}
// FOR Firefox,Chrome
else {
fileSize = f.files.item(0).size;
checkSize();
}
}
// 检查文件大小
function checkSize() {
// FOR IE FIX
if ($.browser.msie) {
fileSize = this.fileSize;
}
if (fileSize > SizeLimit) {
alert('文件超过大小');
} else {
document.FileForm.submit();
}
}
</script>