图片上传前用js客户端检测
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>Untitled</title>
<script>
var img=null;
var maxfilesize = 1048576;
var filePath;
function checkimage()
{
filePath = document.getElementById("FILE1").value; //得到上传控件中的内容
if(/^.+\.(gif|jpg|png)$/i.test(filePath)){
img = new Image();
img.style.display='none';
img.attachEvent("onerror",ErrImgType);
img.attachEvent("onreadystatechange",ErrImgBig);
document.body.insertAdjacentElement("beforeend",img);
img.src=filePath;
}else{ErrImgType()}
}
function ErrImgType(){
upload.reset(); //重置上传控件
alert("上传图片文件类型只能是jpg,png或gif!");
return false;
}
function ErrImgBig(){
if (img.fileSize>maxfilesize){
upload.reset(); //重置上传控件
alert("文件大小不能超过1M字节!");
return false;
}
}
</script>
</head>
<body>
<FORM METHOD="post" id="upload" NAME="upload" ENCTYPE="multipart/form-data">
<input type="file" id="FILE1" name="FILE1" class="bg4" onChange="setTimeout('checkimage()',500)">
</form>
</body>
</html>
<html>
<head>
<title>Untitled</title>
<script>
var img=null;
var maxfilesize = 1048576;
var filePath;
function checkimage()
{
filePath = document.getElementById("FILE1").value; //得到上传控件中的内容
if(/^.+\.(gif|jpg|png)$/i.test(filePath)){
img = new Image();
img.style.display='none';
img.attachEvent("onerror",ErrImgType);
img.attachEvent("onreadystatechange",ErrImgBig);
document.body.insertAdjacentElement("beforeend",img);
img.src=filePath;
}else{ErrImgType()}
}
function ErrImgType(){
upload.reset(); //重置上传控件
alert("上传图片文件类型只能是jpg,png或gif!");
return false;
}
function ErrImgBig(){
if (img.fileSize>maxfilesize){
upload.reset(); //重置上传控件
alert("文件大小不能超过1M字节!");
return false;
}
}
</script>
</head>
<body>
<FORM METHOD="post" id="upload" NAME="upload" ENCTYPE="multipart/form-data">
<input type="file" id="FILE1" name="FILE1" class="bg4" onChange="setTimeout('checkimage()',500)">
</form>
</body>
</html>