菜单

澳门永利304网站图片上传即时显示缩略图的js代码

2019年9月21日 - 电脑百科
<script language="javascript" type="text/javascript">
 var allowExt = ['jpg', 'gif', 'bmp', 'png', 'jpeg'];
 var preivew = function(file, container){
  try{
  var pic = new Picture(file, container);
  }catch(e){
  alert(e);
  }
 }
 //缩略图类定义
   var Picture = function(file, container){
  var height = 0, 
  widht = 0, 
  ext = '',
  size = 0,
  name  = '',
  path  = '';
  var self = this;
  if(file){ 
    name = file.value;
  if (window.navigator.userAgent.indexOf("MSIE")>=1){ 
   file.select(); 
   path = document.selection.createRange().text; 
  }else if(window.navigator.userAgent.indexOf("Firefox")>=1){ 
   if(file.files){ 
   path = file.files.item(0).getAsDataURL(); 
   }else{
   path = file.value;
   }
  } 
  }else{
  throw "bad file";
  } 


  ext = name.substr(name.lastIndexOf("."), name.length);

  if(container.tagName.toLowerCase() != 'img'){
  throw "container is not a valid img label";
  container.visibility = 'hidden';
  }
  container.src = path;
  container.alt = name;
  container.style.visibility = 'visible';
  height = container.height;
  widht = container.widht;
  size  = container.fileSize;


  this.get = function(name){
  return self[name];
  }

  this.isValid = function(){
  if(allowExt.indexOf(self.ext) !== -1){
   throw 'the ext is not allowed to upload';
   return false;
  }
  }
 }
</script>
  <div class='previewDemo'>
 <input id="file" type="file" onchange="preivew(this, document.getElementById('img'));">
 <img id="img" style="visibility:hidden" height="100px" width="100px">
</div>

JS多图片上传小小实例:

您可能感兴趣的文章:

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <title>上传多图片</title>
  <script type="text/javascript">
    function JM_wu(ob) {
      ob.style.display = "none";
    }
    function JM_you(ob) {
      ob.style.display = "";
    }
    function createForm(textN, number) {
      data = "";
      inter = "'";
      if (number < 11 && number > -1) {
        for (i = 1; i <= number; i++) {
          if (i < 10) spaces = " ";
          else spaces = " ";
          data = data + " <input name=" + textN + i + " type=text value='' size='30' maxlength='100'>"
          + "<INPUT TYPE='button' value='上传" + i + "' onclick=javascript:uppic('more" + i + "','more" + i + "')>";
        }
        if (document.layers) {
          document.layers.cust.document.write(data);
          document.layers.cust.document.close();
        }
        else {
          if (document.all) {
            cust.innerHTML = data;
          }
        }
      }
      else {
        window.alert("请不要超过10张图片.");
      }
    }
</script>
</head>
<body>
  <form name="prodtable" action="">
  <input type="radio" name="more_pic" checked value="0" onclick="JM_wu(s1)">无
  <input type="radio" name="more_pic" value="1" onclick="JM_you(s1)">有 
    <input name="more" type="text" value='1' size='5' maxlength='2'>
    <input type="button" value="张大图" onclick="createForm('more',document.prodtable.more.value);">

  </form>
</body>
</html>

这只是一个指引的小例子,同学们可以把按钮换成浏览图片按钮。

澳门永利304网站,您可能感兴趣的文章:

相关文章

发表评论

电子邮件地址不会被公开。 必填项已用*标注

网站地图xml地图