本地、远程图片上传预览

<title>文件上传前台控制检测程序v0.6</title>
<style>
body,td{font-size:12px;}
</style>
<scriptlanguage=javascript>

/*----------------------------------------
文件上传前台控制检测程序v0.6

远程图片检测功能
检测上传文件类型

 检测图片文件格式是否正确
 检测图片文件大小
 检测图片文件宽度
 检测图片文件高度
图片预览

For51js.comAuthor:333Date:2005/08/26
UpDate:2005/09/03
-----------------------------------------*/

varImgObj=newImage();//建立一个图像对象

varAllImgExt=".jpg|.jpeg|.gif|.bmp|.png|"//全部图片格式类型
varFileObj,ImgFileSize,ImgWidth,ImgHeight,FileExt,ErrMsg,FileMsg,HasCheked,IsImg//全局变量图片相关属性

//以下为限制变量
varAllowExt=".jpg|.gif|.doc|.txt|"//允许上传的文件类型&#320;为无限制每个扩展名后边要加一个"|"小写字母表示
//varAllowExt=0
varAllowImgFileSize=70;//允许上传图片文件的大小0为无限制单位:KB
varAllowImgWidth=500;//允许上传的图片的宽度&#320;为无限制 单位:px(像素)
varAllowImgHeight=500;//允许上传的图片的高度&#320;为无限制 单位:px(像素)

HasChecked=false;

functionCheckProperty(obj)//检测图像属性
{
FileObj=obj;
if(ErrMsg!="")//检测是否为正确的图像文件 返回出错信息并重置
{
ShowMsg(ErrMsg,false);
returnfalse;//返回
}

if(ImgObj.readyState!="complete")//如果图像是未加载完成进行循环检测
{
setTimeout("CheckProperty(FileObj)",500);
returnfalse;
}

ImgFileSize=Math.round(ImgObj.fileSize/1024*100)/100;//取得图片文件的大小
ImgWidth=ImgObj.width//取得图片的宽度
ImgHeight=ImgObj.height;//取得图片的高度
FileMsg="\n图片大小:"+ImgWidth+"*"+ImgHeight+"px";
FileMsg=FileMsg+"\n图片文件大小:"+ImgFileSize+"Kb";
FileMsg=FileMsg+"\n图片文件扩展名:"+FileExt;

if(AllowImgWidth!=0&&AllowImgWidth<ImgWidth)
ErrMsg=ErrMsg+"\n图片宽度超过限制。请上传宽度小于"+AllowImgWidth+"px的文件,当前图片宽度为"+ImgWidth+"px";

if(AllowImgHeight!=0&&AllowImgHeight<ImgHeight)
ErrMsg=ErrMsg+"\n图片高度超过限制。请上传高度小于"+AllowImgHeight+"px的文件,当前图片高度为"+ImgHeight+"px";

if(AllowImgFileSize!=0&&AllowImgFileSize<ImgFileSize)
ErrMsg=ErrMsg+"\n图片文件大小超过限制。请上传小于"+AllowImgFileSize+"KB的文件,当前文件大小为"+ImgFileSize+"KB";

if(ErrMsg!="")
ShowMsg(ErrMsg,false);
else
ShowMsg(FileMsg,true);
}

ImgObj.οnerrοr=function(){ErrMsg='\n图片格式不正确或者图片已损坏!'}

functionShowMsg(msg,tf)//显示提示信息tf=true显示文件信息tf=false显示错误信息msg-信息内容
{
msg=msg.replace("\n","<li>");
msg=msg.replace(/\n/gi,"<li>");
if(!tf)
{
document.all.UploadButton.disabled=true;
FileObj.outerHTML=FileObj.outerHTML;
MsgList.innerHTML=msg;
HasChecked=false;
}
else
{
document.all.UploadButton.disabled=false;
if(IsImg)
PreviewImg.innerHTML="<imgsrc='"+ImgObj.src+"'width='60'height='60'>"
else
PreviewImg.innerHTML="非图片文件";
MsgList.innerHTML=msg;
HasChecked=true;
}
}

functionCheckExt(obj)
{
ErrMsg="";
FileMsg="";
FileObj=obj;
IsImg=false;
HasChecked=false;
PreviewImg.innerHTML="预览区";
if(obj.value=="")returnfalse;
MsgList.innerHTML="文件信息处理中...";
document.all.UploadButton.disabled=true;
FileExt=obj.value.substr(obj.value.lastIndexOf(".")).toLowerCase();
if(AllowExt!=0&&AllowExt.indexOf(FileExt+"|")==-1)//判断文件类型是否允许上传
{
ErrMsg="\n该文件类型不允许上传。请上传"+AllowExt+"类型的文件,当前文件类型为"+FileExt;
ShowMsg(ErrMsg,false);
returnfalse;
}

if(AllImgExt.indexOf(FileExt+"|")!=-1)//如果图片文件,则进行图片信息处理
{
IsImg=true;
ImgObj.src=obj.value;
CheckProperty(obj);
returnfalse;
}
else
{
FileMsg="\n文件扩展名:"+FileExt;
ShowMsg(FileMsg,true);
}

}

functionSwitchUpType(tf)
{
if(tf)
str='<inputtype="file"name="file1"οnchange="CheckExt(this)"style="width:180px;">'
else
str='<inputtype="text"name="file1"οnblur="CheckExt(this)"style="width:180px;">'
document.all.file1.outerHTML=str;
document.all.UploadButton.disabled=true;
MsgList.innerHTML="";
}

</script>
<formenctype="multipart/form-data"method="POST"οnsubmit="returnHasChecked;">
<fieldsetstyle="width:372;height:60;padding:2px;">
<legend><fontcolor="#FF0000">图片来源</font></legend>
<inputtype="radio"name="radio1"checkedοnclick="SwitchUpType(true);">本地<inputtype="radio"name="radio1"οnclick="SwitchUpType(false);">远程:<inputtype="file"name="file1"οnchange="CheckExt(this)"style="width:180px;"><inputtype="submit"id="UploadButton"value="开始上传"disabled><br>
<divstyle="border:1solid#808080;background:#E0E0E0;width100%;height:20px;color:#606060;padding:5px;">
<tableborder="0"><tr><tdwidth="60"id="PreviewImg">预览区</td><tdid="MsgList"valign="top"></td></tr></table>
</div>
</fieldset>
</form>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值