HTML5連續上傳圖片

來源:互聯網
上載者:User

標籤:function   html5   files   圖片   

/*by 的雨time:2016/11/17*/function update_object(){//這是在執行個體化的時候,同時調用int方法this.int.apply(this,arguments);}//這是原型鏈 == 一個對象update_object.prototype={int:function(options){//這是接收從調用的時候傳過來參數this.CC=options.CC;this.tishi=options.tishi;this.BB=options.BB;this.show=options.show;this.myfile=options.myfile;this.array=[];var that=this;this.btn=options.btn;this.myfile.onchange=function()   //input發生改變的時候觸發(onchange事件){//第一步驟,這個步驟是找到file,即上傳的檔案var files = this.files;/*concat() 方法用於串連兩個或多個數組。 把that.filter(files)添加到that.array,這樣that.array就會不斷的儲存file數組該方法不會改變現有的數組,而僅僅會返回被串連數組的一個副本。*/that.array=that.array.concat(that.filter(files));that.Index();return this;}//這是點擊上傳的步驟this.btn.onclick=function(e){that.array;var e=e||event;e.preventDefault();//點擊的同時調用上傳的方法that.upload();}},//第二步驟,這是過濾file的步驟,這一步在concat之前就調用filter: function(files) {var arrFiles=[];for (var i = 0, file; file = files[i]; i++) {if (file.type.indexOf("image") == 0) {//if (file.size >= 512000) {////alert(‘您這張"‘+ file.name +‘"圖片大小過大,應小於500k‘);//} else {arrFiles.push(file);//}} else {alert(‘檔案"‘ + file.name + ‘"不是圖片。‘);}}return arrFiles;},//第三步驟,這是為每個file添加索引,在concat串連之後調用,把之前的this.array的內容改變了Index: function() {for (var i = 0, file; file = this.array[i]; i++) {//增加唯一索引值file.index = i;}//這裡的this.array已經有索引this.onSelect(this.array);return this;},//第四步驟,是產生img預覽和刪除預覽onSelect: function(files) {var that=this;var html = ‘‘, i = 0;//動態建立img和livar show1 = function() {file = files[i];if (file){//var reader = new FileReader()var URL=window.URL.createObjectURL(file)//reader.onload = function(e) //{html+=‘<li ><div id="jindu">上傳成功</div><img id="pic_‘ + i + ‘" src=‘+URL+‘><span id="name_‘+ i +‘" class="upload_append_list">‘+file.name+‘</span><a href="#" id="del" title="刪除" index="‘+ i +‘">×</a></li>‘;//console.log(file);i++;show1();that.show.style.display=‘block‘;that.show.innerHTML=html;//}//reader.readAsDataURL(file);e.target.result} };show1();//這是刪除預覽,同時把已經刪除的file的索引傳到下一個數組var del=function(){if (this.show.hasChildNodes()) {var Li=this.show.getElementsByTagName(‘li‘);var length=this.show.childNodes.length;for(var i=0;i<length;i++){Li[i].onmouseover=function(){this.lastChild.style.display=‘block‘;this.lastChild.onclick=function(){this.parentNode.parentNode.removeChild(this.parentNode);var a=this.getAttribute("index"); //這一步找到索引,因為file和a索引都是一樣,找到a等於找到filethat.picdelete(files[a]);  //這部分已經是刪除的file,傳遞到下一個數組}}Li[i].onmouseout=aa=function(){this.lastChild.style.display=‘none‘;}}}}del();},//第五步驟,這是刪除選擇的file的步驟picdelete:function(a){var arr=[];for(var i=0,file;file=this.array[i];i++){if(a!==file) //遍曆this.array找到和a相同的,就不要把它儲存到數組{arr.push(file);}}this.array=arr;  //把最後的file對象內容重新賦值給this.array(已不是剛開始的那個值)},//第六步驟,這是上傳的upload:function(){//this.array是對象,不是數組var that=this; var formData = new FormData();  //這是HTML5的上傳,能夠上傳圖片和文字var aaaa//這一步,把所有的this.array都轉換為二維數組,例file1,file2,file3,方便最後一步全部上傳,不用每迴圈一次就上傳一次for (var i = 0, file; file = this.array[i]; i++) {formData.append("file"+i,file); //要加i否則就會被覆蓋,只有最後一個值//aaaa=i;console.log(i);}aaaa=aaaa+1; var xhr = new XMLHttpRequest(); //這這部分是顯示上傳進度條的xhr.upload.onprogress=function(evt){var lod=evt.loaded;   //已經上傳的大小var to=evt.total;   //總的大小var per=Math.floor(((lod/to))*100)+"%";that.tishi.style.display=‘block‘;that.tishi.innerHTML=‘你上傳了‘+(aaaa*Math.floor(((lod/to))))+‘張照片;‘+‘已經上傳‘+per;if(per==‘100%‘){var Li=that.show.childNodes;for(var i=0;i<Li.length;i++){Li[i].firstChild.style.display=‘block‘;Li[i].onmouseover=function(){this.lastChild.style.display=‘none‘;}}}}//接收後台返回來的資料        xhr.onreadystatechange = function(){            if(xhr.readyState==4&&xhr.status==200){               console.log(xhr.responseText)            }        }        xhr.open(‘POST‘,‘check.php‘,true);        //xhr.setRequestHeader(‘Content-Type‘, ‘application/x-www-form-urlencoded‘);        xhr.send(formData);//傳過去的值,用$_FILES接受,相當於直接表單提交/*步驟1,先找到file檔案,過濾後用新的數組串連成一個數組2,為每個file添加一個i值。就是索引;3,從得到索引的file遍曆處理,動態建立img4,刪除事件,把選擇刪除的file傳遞到下一級5,重新組合file組合,重新遍曆,不儲存上級帶有刪除的file6,最後得到的是確定要上傳的file組合,一般和開始的數組不一樣*/}}

  這是調用的

<script>window.onload=function(){var CC=document.getElementById(‘cc‘);var BB=document.getElementById(‘bb‘);var tishi=document.getElementById(‘tishi‘);var show=document.getElementById(‘show‘);var myfile=document.getElementById(‘myfile‘);var btn=document.getElementById(‘submit‘);var update=new update_object({CC:CC,BB:BB,tishi:tishi,show:show,myfile:myfile,btn:btn});}</script>

 

<form action="check.php"  method="post" enctype="multipart/form-data"><div id="aa"><div id=bb>    <label>    <div id="cc" title="上傳圖片">上傳圖片</div>        <input type="file" id="myfile" name=‘name[]‘ accept="image/jpeg,image/jpg,image/png,image/gif" style=" display:none"  multiple=‘true‘>      </label>    </div>     <div id="size">    <div  id="tishi">    </div>    <label>    <div id="begin">開始上傳</div>    <input id=‘submit‘ type="submit" style="display:none" value="">    </label>    </div>    <ul id="show">    </ul></div><input id=‘submit‘ type="submit" value="提交"></form>

 個人是新手,所以寫的代碼不規範,還請多多包涵。我也是在網上找了很久,找不到全是純JavaScript寫的,所以想把這篇,讓新手學習。

本文出自 “11037915” 部落格,請務必保留此出處http://11047915.blog.51cto.com/11037915/1874039

HTML5連續上傳圖片

聯繫我們

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