1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
2 <HTML>
3 <HEAD>
4 <TITLE> New Document </TITLE>
5 <META NAME="Generator" CONTENT="EditPlus">
6 <META NAME="Author" CONTENT="">
7 <META NAME="Keywords" CONTENT="">
8 <META NAME="Description" CONTENT="">
9 <script>
10 var fileSize=160;
11 var fileType="jpg,gif,bmp,png";
12 var bool=false; //0代表上传的类型不对,1代表上传的类型正确
13 var str="";
14 function checkform(){
15 str=document.getElementById("photo").value.toLowerCase();
16 var ext=str.split(".");
17 var type=fileType.split(",");
18 for(var i=0;i<type.length;i++){
19 alert(ext[1]+","+type[i]);
20 if(ext[1]==type[i]){
21 bool=true;
22 alert(bool);
23 }
24 }
25 if(bool){
26 var boolSize=checkSize(str);
27 alert(boolSize);
28 if(boolSize){
29 alert("上传成功");
30 }else{
31 alert("你上传的图片太大,不能超过"+fileSize+"KB");
32 }
33 }else{
34 bool=false;
35 alert("你上传的类型不是"+type+"中的一种");
36 }
37 }
38 function checkSize(fileName){
39 var img=new Image();
40 img.src=str;
41 fsize=img.fileSize/1024;
42 fsize=fsize.toFixed(1);
43 if(img.readyState!="complete"){
44 bool=false;
45 return false;
46 }else{
47 alert(fsize+","+fileSize);
48 if(fsize>fileSize){
49 bool=false;
50 return false;
51 }else{
52 bool=false;
53 return true;
54 }
55 }
56 }
57 </script>
58 </HEAD>
59
60 <BODY>
61 <form name= "form1 " action= " ">
62 照片: <input type="file" name= "photo" id="photo" value= " " onchange= "checkform();">
63 </form>
64 </BODY>
65 </HTML>
66