jquery驗證上傳圖片且帶大小驗證與圖片預覽效果

來源:互聯網
上載者:User

jquery驗證上傳圖片且帶大小驗證與圖片預覽效果
*/

function submit_upload_picture(){
 var file = $('file_c').value;
 if(!/.(gif|jpg|jpeg|png|gif|jpg|png)$/.test(file)){
        alert("圖片類型必須是.gif,jpeg,jpg,png中的一種")
    }else{
  $('both_form').action="file!upload.action";
  $('both_form').submit();
  $('insert_img').sethtml('<img src="yun_qi_img/loading.gif"/>');
  $('display_div').setstyle('display', 'block');
  $('upload_div').setstyle('display', 'none');
 }
}

//執行個體二

function validate_edit_logo(a){
 var file = $('file').value;
 if(!/.(gif|jpg|jpeg|png|gif|jpg|png)$/.test(file)){
        alert("圖片類型必須是.gif,jpeg,jpg,png中的一種")
        if(a==1){
         return false;
        }
    }else{
     var image = new image();
     image.src = file;
     var height = image.height;
     var width = image.width;
     var filesize = image.filesize;
     $('beforeend').src=file;
     $('div_regi_right').setstyle('display', 'block');
     if(width>80 && height>80 && filesize>102400){
      alert('請上傳80*80像素 或者大小小於100k的圖片');
      if(a==1){
       return false;
      }
     }
     if(a==1){
      return true;
     }
 }
}

//執行個體三

function viewimg(index) {
 var name = 'uploadimg' + index;
 var imgup = $(name);
 var imgpath = getpath(imgup);
 var   local   =   imgup.value;
 var   point   =   local.lastindexof(".");
 //判斷上傳檔案大小
 var img   =   document.createelement("img");    
 img.src   =   local;
 var filesize = img.filesize;
 img.onload = function(){filesize=this.filesize;}
 if(img.filesize>5242880){ 
  alert("圖片檔案過大!");
  return   false;
 }
 
 //判斷是否是圖片格式
 var imgname = imgup.value.substring(imgup.value.lastindexof("."), imgup.value.length);
 imgname = imgname.tolowercase();
 if ((imgname != ".jpg") && (imgname != ".gif") && (imgname != ".jpeg") && (imgname != ".png") && (imgname != ".bmp")) {
  alert("u8bf7u9009u62e9u56feu7247u6587u4ef6uff0cu8c22u8c22!");
  imgup.focus();
  //清空file裡面的值
  imgup.select();
  document.selection.clear();
 } else {
 //顯示圖片
  document.getelementbyid("sig_preview"+index).innerhtml = "<img src='" + imgpath + "' border=0 width=200 height=150><img src='images/u51.png' width='16' height='14' onclick='delimage(" + index + ");' />";
 }
 if (index >=3){
  var cnt = index + 1;
  $("img" + cnt).style.display = "";
 }
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.