1 //检测文件大小和类型
2 function fileChange(target){
3 //检测上传文件的类型
4 if(!(/(?:jpg|gif|png|jpeg)$/i.test(target.value))) {
5 alert("只允许上传jpg|gif|png|jpeg格式的图片");
6 if(window.ActiveXObject) {//for IE
7 target.select();//select the file ,and clear selection
8 document.selection.clear();
9 } else if(window.opera) {//for opera
10 target.type="text";target.type="file";
11 } else target.value="";//for FF,Chrome,Safari
12 return;
13 } else {
14 return; //alert("ok");//or you can do nothing here.
15 }
16
17 //检测上传文件的大小
18 var isIE = /msie/i.test(navigator.userAgent) && !window.opera;
19 var fileSize = 0;
20 if (isIE && !target.files){
21 var filePath = target.value;
22 var fileSystem = new ActiveXObject("Scripting.FileSystemObject");
23 var file = fileSystem.GetFile (filePath);
24 fileSize = file.Size;
25 } else {
26 fileSize = target.files[0].size;
27 }
28 var size = fileSize / 1024;
29 if(size>(500)){
30 alert("文件大小不能超过500KB");
31 if(window.ActiveXObject) {//for IE
32 target.select();//select the file ,and clear selection
33 document.selection.clear();
34 } else if(window.opera) {//for opera
35 target.type="text";target.type="file";
36 } else {
37 target.value="";//for FF,Chrome,Safari
38 }
39 return;
40 }else{
41 return;
42 }
43 }