javascript上传并限制图片大小

 

<head>
<title>图片上传</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">

<script language="javascript">
<!--
window.focus();

window.οnlοad=function (){              
RadioClick("file")
}
// 图片来源单选点击事件
function RadioClick(what){
 if (what=="url"){                     //选择远程获取
 thisform.button.disabled=true;
 thisform.button.value="・远程获取・";
 thisform.wang1.disabled=false;        //远程获取输入框可用
 thisform.wang.checked=true;           //远程获取Radio 点上
 document.all.file.select();
 document.execCommand("Delete");       //清除浏览框的数据
 document.getElementById("err_msg").innerHTML=""
 document.getElementById("ErrMsg").innerHTML=""
 thisform.shang1.disabled=true;        //使浏览框不可用
    thisform.action="upfile.asp?act=ychq"
    thisform.encoding="application/x-www-form-urlencoded";
 FileChange();
 
 }else{
 thisform.button.disabled=true;
 thisform.shang1.disabled=false;
 thisform.shang.checked=true;
 thisform.button.value="・开始上传・";
 document.all.file1.select();
 document.execCommand("Delete");       //清除浏览框的数据
 document.thisform.file1.value='';
 document.getElementById("err_msg").innerHTML=""
 document.getElementById("ErrMsg").innerHTML=""
 thisform.wang1.disabled=true;
 thisform.action="upfile.asp"
    thisform.encoding="multipart/form-data";
 }
}
-->
</script>

<script language="JavaScript">
var AllowImgFileSize=100;  //允许上传图片文件的大小 0为无限制  单位:KB
var AllowImgWidth=500;   //允许上传的图片的宽度 ?为无限制 单位:px(像素)
var AllowImgHeight=500;   //允许上传的图片的高度 ?为无限制 单位:px(像素)
function load(Obj){
var tempImg=new Image();
tempImg.οnerrοr=function(){                   //不是图片
 thisform.button.disabled=true;            //提交不可用
 Obj.outerHTML=Obj.outerHTML;              //清除表单
 document.getElementById("err_msg").innerHTML="目标类型格式不正确或者图片已损坏!"
 document.getElementById("ErrMsg").innerHTML=""
};
tempImg.onreadystatechange=function(){
 thisform.button.disabled=false;          //提交可用
 document.getElementById("err_msg").innerHTML="&nbsp;&nbsp;&nbsp; 图片宽度:<font color=red>" +this.width + "</font><br>&nbsp;&nbsp;&nbsp; 图片高度:<font color=red>" + this.height + "</font><br>&nbsp;&nbsp;&nbsp; 图片大小:<font color=red>" + parseInt(this.fileSize/1024) + "K</font>";

 if(AllowImgWidth!=0&&AllowImgWidth<this.width ||AllowImgHeight!=0&&AllowImgHeight<this.height ||AllowImgFileSize!=0&&AllowImgFileSize*1024<this.fileSize )
   {
   document.all.file1.select();
   document.execCommand("Delete");      
   Obj.outerHTML=Obj.outerHTML;
   document.getElementById("ErrMsg").innerHTML="/n图片不要超过<font color=red>"+AllowImgFileSize+"</font>KB。<font color=red>"+AllowImgWidth+"</font>X<font color=red>"+AllowImgHeight+"</font>"
    thisform.button.disabled=true;

 }
  else{
     document.getElementById("ErrMsg").innerHTML=""
     document.all.err_msg.style.display='';


  }
  };
   
tempImg.src=Obj.value;
}


function up(){
  if(thisform.button.disabled) event.returnValue=false;
}

</script>
<script language="JavaScript">
var flag=false;
function DrawImage(ImgD){
   var image=new Image();
   image.src=ImgD.src;
   if(image.width>0 && image.height>0){
    flag=true;
    if(image.width/image.height>= 120/80){
     if(image.width>120){
     ImgD.width=120;
     ImgD.height=(image.height*120)/image.width;
     }else{
     ImgD.width=image.width;
     ImgD.height=image.height;
     }
     }
    else{
     if(image.height>80){
     ImgD.height=80;
     ImgD.width=(image.width*80)/image.height;
     }else{
     ImgD.width=image.width;
     ImgD.height=image.height;
     }
     }
    }

  }

function FileChange(Value){
flag=false;
document.all.uploadimage.width=0;
document.all.uploadimage.height=0;
document.all.uploadimage.alt="";
document.all.uploadimage.src=Value;
}
function mysub()
{
 esave.style.visibility="visible"; 
}
</script>
<style>
<!--
body         { font-size: 2; font-family: monospace }
-->
</style>
</head>

<body topmargin="0"style="border:0px;" scroll="no">
<form name="thisform" method="post" οnsubmit="javascript:up();">
 <div id="esave" style="position:absolute; top:18px; left:40px; z-index:10; visibility:hidden">
  <table width="340" border="0" cellspacing="0" cellpadding="0">
   <tr>
    <td width="20%"></td>
    <td bgcolor="#104A7B" width="60%">
    <table width="100%" height="120" border="0" cellspacing="1" cellpadding="0">
     <tr>
      <td bgcolor="#eeeeee" align="center"><marquee align="middle" behavior="alternate" scrollamount="5"><font color="#FF0000">...图片上传中...请等待...</font></marquee></td>
     </tr>
    </table>
    </td>
    <td width="20%"></td>
   </tr>
  </table>
 </div>
 <table width="400" border="0" cellspacing="0" cellpadding="3" align="center">
  <tr>
   <td height="22" align="center" width="400">
   <fieldset style="width: 384; height: 60">
   <legend><font color="#FF0000">图片来源</font></legend>
   <table border="0" cellpadding="0" cellspacing="0" width="380">
    <tr>
     <td  οnclick="RadioClick('file')">
     <input type="radio" id="shang" value="sc" name="lb"  οnclick="RadioClick('file')">本地上传: </td>
     <td>
     <input type="file" id="shang1" name="file" style="width:280'"contentEditable=false class="an3" value size="20" disabled onpropertychange="javascript:load(this);" οnchange="javascript:FileChange(this.value);">
     </td>
    </tr>
    <tr>
     <td οnclick="RadioClick('url')">
     <input type="radio" id="wang" value="wl" name="lb" οnclick="RadioClick('url')">远程获取:</td>
     <td><input type="file" name="file1" id="wang1" style="width:280px"  size="20" disabled class="an3" onpropertychange="javascript:load(this);"></td>
    </tr>
   </table>
   </fieldset></td>
  </tr>
  <tr align="center" valign="middle"> 
   <td height="80" width="400">
   <fieldset style=" height: 80">
   <table border="0" width="100%" cellspacing="0" cellpadding="0">
   <tr>
   <td width=150 align="center"><img id="uploadimage" height="0" width="0" src οnlοad="javascript:DrawImage(this);"></td>
   <td width=250 align="left">
   <div id="err_msg" ></div>
   <div id="ErrMsg" ></div>
   </td>
   </tr>
   </table>
   </fieldset>
   </td>
  </tr>
  <tr align="center" valign="middle">
   <td height="24" width="400">
   <input type="submit" name="button" value disabled class="an1" οnclick="javascript:mysub()">
   </td>
  </tr>
 </table>
</form>

</body>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值