<!DOCTYPE   HTML   PUBLIC   "-//W3C//DTD   HTML   4.01   Transitional//EN">   
  
<html>   
  
<head>   
  
<title>客户端控制上传图像大小示例</title>   
  
<meta   http-equiv="Content-Type"   content="text/html;   charset=gb2312">   
  
</head>   
  
<body>   
  
<p>以下是一个客户端控制file上传图像大小的一个示例,由于是客户端限制,所以用户可以绕过这个限制。</p>   
  
<SCRIPT   LANGUAGE="JavaScript">   
  
<!--   
  
var   FileMaxSize   =   50;//限制上传的文件大小,单位(k)   
  function   chkform(){   
  
if   (document.HwForm.Photo.value==""){alert("请选择照片");document.HwForm.Photo.focus();return   false;}   
  
if(document.HwForm.PhotoFileSize.value<=0){alert("请重新选择照片");document.HwForm.Photo.focus();return   false;}   
  
if(document.HwForm.PhotoFileSize.value>FileMaxSize*1024){alert("选择的照片超过"+FileMaxSize+"K,请重新选择");document.HwForm.Photo.focus();return   false;}   
  alert(
"可以提交,当前为测试");return   false;   
  }
   
  
//-->   
  
</SCRIPT>   
  
<form   method="POST"   name="HwForm"   onsubmit="return(chkform());"   enctype="multipart/form-data">   
  照片(请选择一个50K以内的图片文件):
<input   type="file"   name="Photo"   onpropertychange="if(document.HwForm.Photo.value!=''){document.getElementById('PhotoImg').src=document.HwForm.Photo.value;}"/><IMG   style="POSITION:   absolute;   TOP:   -100000px;left:-100000px;"   onerror="document.all.PhotoFileSize.value='-1';alert('请选择一个图像文件');"   onload="if(document.getElementById('PhotoImg').fileSize<=1024*FileMaxSize){document.all.PhotoFileSize.value=document.getElementById('PhotoImg').fileSize;}else{alert('图片不能大于'+FileMaxSize+'K,请重新选择');document.all.PhotoFileSize.value='-1';}"   id="PhotoImg"><input   size="3"   type="hidden"   name="PhotoFileSize"   value="-1"   />   
  
<input   type="submit"   value="提交">   <input   type="reset"   value="重置">     
  
</form>   
  
</body>   
  
</html>
posted on 2008-04-12 11:37  小角色  阅读(843)  评论(1)    收藏  举报