cjhupload一個簡單非同步檔案上傳外掛程式(html5+js),

來源:互聯網
上載者:User

cjhupload一個簡單非同步檔案上傳外掛程式(html5+js),

來源:http://www.myopenresources.com/page/resource_detail_0.html?rid=370    

更多文章請查看本人部落格網站:http://www.myopenresources.com

cjhupload是一個本人基於原生JS編寫的一個檔案上傳外掛程式,支援手機、電腦端,你可查看例子,或下載詳細例子(請到下面的“”下載)。

例子:

<!DOCTYPE HTML>
<html lang='zh'>
<head>
<meta charset="utf-8">
<meta name="author" content="ISUX">
<meta name="format-detection" content="telephone=no" />
<meta name="viewport" content="width=device-width,user-scalable=no" />
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black" />
<title>demo</title>

        <!--jquery-->
        <script src="jquery.min.js"></script>

        <!--語言套件-->
<script src="fileupload/cjhupload-zh.js"></script>

<!--core-->
<script src="fileupload/cjhupload.js"></script>

<script type="text/javascript">

 //檢查檔案類型
           function checkFileType(file,fileName){
                   var regExp =new RegExp("^.*?\.(jpg|jpeg|png)$");
       if(!regExp.test(fileName)){
         return false;
       }else{
         return true;
       }
           }

  //檔案上傳
  function uploadImg(){
    var tempList=new Array();
    for(var i=0;i<fileList.length;i++){
      tempList.push(fileList[i].file);
    }
    
    var cjhUpload = new CJHUpload({
     url:'../index/upload.jhtml',//檔案上傳地址
     fileList:tempList,
     onSuccess:function(file, responseText){
       alert(responseText);
     },
     onCheckAllFileType:function(file,fileName){
       return checkFileType(file,fileName);
     }
});
 
    cjhUpload.sendFormData();
  }

  //讀取圖片
           var fileId=1;
           var fileList = new Array();
  function readFile(obj){
    if (obj.files && obj.files[0]){
     if(!checkFileType(obj.files[0],obj.files[0].name)){
         $.tips({
       content:'上傳的檔案類型不正確!',
       stayTime:1500,
       type:"warn"
     })
     }else{
        
         
         var fileObject=new Object();
         fileObject.file=obj.files[0];
         fileObject.id=fileId;
     fileList.push(fileObject);
     
     var reader = new FileReader();
     reader.onload = function(evt){
        var liId="liImgId"+fileId;
        $('#imgViews').append('<li id="'+liId+'" onclick="viewAddImg(\''+fileId+'\',\''+(evt.target.result)+'\');"><img src="'+(evt.target.result)+'"/></li>');
        var lisLen=$('#imgViews >li').length;
        $("#imgViews").append($("#imgViews >li").eq(lisLen-2).remove());
     }
             reader.readAsDataURL(obj.files[0]);
             
             reader.onloadend=function(){
               fileId++;
             }
             
    }
    }
  } 

  //提交表單
  function submitData(){
    
    uploadImg();
  }


  </script>
           <style>
li {
  list-style: none; }
.ui-grid-trisect>li {
width: 25%;
float: left;
position: relative;
-webkit-box-sizing: border-box;
text-align: center;
}

.ui-grid-trisect li img {
width: 5.9em;
height: 5.9em;
border: 0.1em solid #e1e1e1;
}

.uwh-bg {
-webkit-background-size: contain;
background-size: contain;
background-repeat: no-repeat;
background-position: center;
background-image: url('add-img.png');
width: 100%;
height: 100%;
cursor: pointer;
}

.uwh-bg input {
width: 5.9em;
height: 5.9em;
filter: alpha(opacity = 0);
opacity: 0;
}

.btn-content {
width: 100%;
height: 10em;
}

.btn-content div {
width: 90%;
height: 2.5em;
margin-left: auto;
margin-right: auto;
line-height: 2.5em;
text-align: center;
font-size: 1.3em;
color: #ffffff;
background-color: orange;
-moz-border-radius: 5px;
-webkit-border-radius: 5px;
border-radius: 5px;
cursor: pointer;
}
  </style>
<body>
       <!-- btn -->
<div class="btn-content" style="margin-top: 2.5em;height: 5em">
<div onclick="submitData();" style="margin-top: 1.1em">
提交
</div>
</div>

  <!-- upload img -->
<div style="margin-top: 0.5em">
  <ul class="ui-grid-trisect" style="padding-left: 1.55%;padding-right:1.55%;text-align: center;" id="imgViews">
     <li class="uwh-bg" id="addImgBtn" style="padding: 0.05em;width:6.0em;height:6.0em">
       <input type="file"  onchange="readFile(this);"/> 
     </li>
     
  </ul>
</div>






</body>
</html>


效果:



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.