JavaScript 处理图片类

[ 2004-09-09 01:00:40 | Author: zhenhua ]
Font Size: Large | Medium | Small
<title>文件上传前台控制检测程序 v0.6</title>
<style>
body,td{font-size:12px;}
</style>
<script language=javascript>

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

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

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

 For 51js.com Author:333 Date:2005/08/26
 UpDate:2005/09/03
-----------------------------------------*/

var ImgObj=new Image(); //建立一个图像对象

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

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

HasChecked=false;

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

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

 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.onerror=function(){ErrMsg='\n图片格式不正确或者图片已损坏!'}

function ShowMsg(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="<img src='"+ImgObj.src+"' width='60' height='60'>"
 else
 PreviewImg.innerHTML="非图片文件";
 MsgList.innerHTML=msg;
 HasChecked=true;
 }
}

function CheckExt(obj)
{
 ErrMsg="";
 FileMsg="";
 FileObj=obj;
 IsImg=false;
 HasChecked=false;
 PreviewImg.innerHTML="预览区";
 if(obj.value=="")return false;
 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);
 return false;
 }

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

function SwitchUpType(tf)
{
 if(tf)
 str='<input type="file" name="file1" onchange="CheckExt(this)" style="width:180px;">'
 else
 str='<input type="text" name="file1" onblur="CheckExt(this)" style="width:180px;">'
 document.all.file1.outerHTML=str;
 document.all.UploadButton.disabled=true;
 MsgList.innerHTML="";
}

</script>
<form enctype="multipart/form-data" method="POST" onsubmit="return HasChecked;">
<fieldset style="width: 372; height: 60;padding:2px;">
<legend><font color="#FF0000">图片来源</font></legend>
<input type="radio" name="radio1" checked onclick="SwitchUpType(true);">本地<input type="radio" name="radio1" onclick="SwitchUpType(false);">远程:<input type="file" name="file1" onchange="CheckExt(this)" style="width:180px;"> <input type="submit" id="UploadButton" value="开始上传" disabled><br>
<div style="border:1 solid #808080;background:#E0E0E0;width100%;height:20px;color:#606060;padding:5px;">
<table border="0"><tr><td width="60" id="PreviewImg">预览区</td><td id="MsgList" valign="top"></td></tr></table>
</div>
</fieldset>
</form>

Comments Feed Comments Feed: http://www.zhenhua.org/feed.asp?q=comment&id=169
UTF-8 Encoding Trackback URL: http://www.zhenhua.org/trackback.asp?id=169

View Mode: Show All | Comments: 0 | Trackbacks: 382 | Toggle Order | Views: 32796
Trackback i need cheap ativan
[ i need cheap ativan | 2012-11-25 02:47:50 ]
Brand and Generic Drugs, Discount Pharmacy. All information about i need cheap ativan in one place: http://perso.wanadoo.es/cendes/ativan/i-need-cheap-ativan.html
Trackback online pharmacies
[ online pharmacies | 2012-11-28 02:53:48 ]
Brand and Generic Drugs, Discount Pharmacy. All information about online pharmacies in one place: http://hoppe.snn.gr/fluconazole/online-pharmacies.html
Trackback provera online
[ provera online | 2012-12-01 01:15:51 ]
Brand and Generic Drugs, Discount Pharmacy. All information about provera online in one place: http://perso.wanadoo.es/mayin/provera/provera-online.html
Trackback bactrim ds remedy
[ bactrim ds remedy | 2013-01-07 20:34:59 ]
Brand and Generic Drugs, Discount Pharmacy. All information about bactrim ds remedy in one place: http://mitglied.multimania.de/drezic/bactrim-ds/bactrim-ds-remedy.html
Trackback elizabeth celebrex
[ elizabeth celebrex | 2013-01-20 22:51:13 ]
Brand and Generic Drugs, Discount Pharmacy. All information about elizabeth celebrex in one place: http://membres.multimania.fr/guesle/celebrex/elizabeth-celebrex.html
Trackback cheap tabs
[ cheap tabs | 2013-02-21 03:55:13 ]
Brand and Generic Drugs, Discount Pharmacy. All information about cheap tabs in one place: http://digilander.libero.it/villasolaro/levodopa/cheap-tabs.html
Trackback valvular heart disease
[ valvular heart disease | 2013-02-24 06:27:10 ]
Brand and Generic Drugs, Discount Pharmacy. All information about valvular heart disease in one place: http://perso.wanadoo.es/superocio3/phentermine/valvular-heart-disease.html
Trackback buy norco online cheap
[ buy norco online cheap | 2013-04-01 19:57:06 ]
Brand and Generic Drugs, Discount Pharmacy. All information about buy norco online cheap in one place: http://digilander.libero.it/albasacco/norco/buy-norco-online-cheap.html
Trackback buy prednisone for dogs
[ buy prednisone for dogs | 2013-04-18 07:09:15 ]
Brand and Generic Drugs, Discount Pharmacy. All information about buy prednisone for dogs in one place: http://utenti.multimania.it/wodich/prednisone/buy-prednisone-for-dogs.html
Trackback celexa
[ celexa | 2013-06-09 22:23:44 ]
Brand and Generic Drugs, Discount Pharmacy. All information about celexa in one place: http://digilander.libero.it/papa81/celexa/
Trackback tuscaloosa xanax
[ tuscaloosa xanax | 2013-06-13 18:37:37 ]
Brand and Generic Drugs, Discount Pharmacy. All information about tuscaloosa xanax in one place: http://tabprice.world.mu/xanax/tuscaloosa-xanax.html
Trackback denver fioricet
[ denver fioricet | 2013-06-29 11:45:29 ]
Brand and Generic Drugs, Discount Pharmacy. All information about denver fioricet in one place: http://med-inf.de-info.de/fioricet/denver-fioricet.html
Trackback preterm labor
[ preterm labor | 2013-07-04 01:49:29 ]
Brand and Generic Drugs, Discount Pharmacy. All information about preterm labor in one place: http://rumohr.pluto.ro/adalat/preterm-labor.html
Trackback buy avodart india
[ buy avodart india | 2013-07-13 09:57:59 ]
Brand and Generic Drugs, Discount Pharmacy. All information about buy avodart india in one place: http://maschs.altervista.org/avodart/buy-avodart-india.html
Trackback esgic for migraines
[ esgic for migraines | 2013-08-02 08:22:47 ]
Brand and Generic Drugs, Discount Pharmacy. All information about esgic for migraines in one place: http://perso.wanadoo.es/septiembre112006/esgic/esgic-for-migraines.html
Trackback tramadol hcl dosage
[ tramadol hcl dosage | 2013-08-08 07:23:39 ]
Brand and Generic Drugs, Discount Pharmacy. All information about tramadol hcl dosage in one place: http://digilander.libero.it/claudiaborni/tramadol-hcl/tramadol-hcl-dosage.html
Trackback adjunctive therapy buspar
[ adjunctive therapy buspar | 2013-08-23 04:41:06 ]
Brand and Generic Drugs, Discount Pharmacy. All information about adjunctive therapy buspar in one place: http://hem.passagen.se/twixstv/buspar/adjunctive-therapy-buspar.html
Trackback corticosteroids dermatologic
[ corticosteroids dermatologic | 2013-09-02 04:29:07 ]
Brand and Generic Drugs, Discount Pharmacy. All information about corticosteroids dermatologic in one place: http://webnode.kilu.de/clobetasol/corticosteroids-dermatologic.html
Trackback thyroid scan
[ thyroid scan | 2013-10-09 07:57:39 ]
Brand and Generic Drugs, Discount Pharmacy. All information about thyroid scan in one place: http://hem.passagen.se/hara1991/synthroid/thyroid-scan.html
Trackback where can i get clomiphene
[ where can i get clomiphene | 2013-10-11 08:03:05 ]
Brand and Generic Drugs, Discount Pharmacy. All information about where can i get clomiphene in one place: http://sanara.xhost.ro/clomiphene/where-can-i-get-clomiphene.html

If you feel this site you find this information helpful, please click on the donation, which is voluntary,Thank you.
Post Comment
Smilies
[arrow] [biggrin] [confused] [cool]
[cry] [eek] [evil] [exclaim]
[frown] [idea] [lol] [mad]
[mrgreen] [neutral] [question] [razz]
[redface] [rolleyes] [sad] [smile]
[surprised] [twisted] [wink]
Enable UBB Codes
Auto Convert URL
Show Smilies
Hidden Comment
Username:   Password:   Register Now?
Security Code * Please Enter the Security Code