js實(shí)現(xiàn)分割上傳大文件
來(lái)源:易賢網(wǎng) 閱讀:2428 次 日期:2016-07-21 15:34:40
溫馨提示:易賢網(wǎng)小編為您整理了“js實(shí)現(xiàn)分割上傳大文件”,方便廣大網(wǎng)友查閱!

本文實(shí)例介紹了js上傳文件操作,分享給大家供大家參考,具體內(nèi)容如下

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" lang="zh-CN">

<head>

<title>分割大文件上傳</title>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<style>

 #test{

  width: 200px;

  height: 100px;

  border: 1px solid green;

  display: none;

 }

 #img{

  width: 50px;

  height: 50px;

  display: none;

 }

 #upimg{

  text-align: center;

  font: 8px/10px '微軟雅黑','黑體',sans-serif;

  width: 300px;

  height: 10px;

  border: 1px solid green;

 }

 #load{

  width: 0%;

  height: 100%;

  background: green;

  text-align: center;

 }

</style>

</head>

 <body>

  <form enctype="multipart/form-data" action="file.php" method="post">

   <!-- 

   <input type="file" name="pic" />

   <div id="img"></div>

   <input type="button" value="uploadimg" onclick="upimg();" /><br />

   -->

   <div id="upimg">

    <div id="load"></div>

   </div>

   <input type="file" name="mof" multiple="multiple"/>

   <input type="button" value="uploadfile" onclick="upfile();" />

   <input type="submit" value="submit" />

  </form>

  <div id="test">

   測(cè)試是否DIV消失

  </div>

<script type="text/javascript">

 var dom=document.getElementsByTagName('form')[0];

 dom.onsubmit=function(){

  //return false;

 }

 var xhr=new XMLHttpRequest();

 var fd;

 var des=document.getElementById('load');

 var num=document.getElementById('upimg');

 var file;

 const LENGTH=10*1024*1024;

 var start;

 var end;

 var blob;

 var pecent;

 var filename;

 //var pending;

 //var clock;

 function upfile(){

  start=0;

  end=LENGTH+start;

  //pending=false;

  file=document.getElementsByName('mof')[0].files[0];

  //filename = file.name;

  if(!file){

   alert('請(qǐng)選擇文件');

   return;

  }

  //clock=setInterval('up()',1000);

  up();

 }

 function up(){

  /*

  if(pending){

   return;

  }

  */

  if(start<file.size){

   xhr.open('POST','file.php',true);

   //xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');

   xhr.onreadystatechange=function(){

    if(this.readyState==4){

     if(this.status>=200&&this.status<300){

      if(this.responseText.indexOf('failed') >= 0){

       //alert(this.responseText);

       alert('文件發(fā)送失敗,請(qǐng)重新發(fā)送');

       des.style.width='0%';

       //num.innerHTML='';

       //clearInterval(clock);

      }else{

       //alert(this.responseText)

       // pending=false;

       start=end;

       end=start+LENGTH;

       setTimeout('up()',1000);

      }

     }

    }

   }

   xhr.upload.onprogress=function(ev){

    if(ev.lengthComputable){

     pecent=100*(ev.loaded+start)/file.size;

     if(pecent>100){

      pecent=100;

     }

     //num.innerHTML=parseInt(pecent)+'%';

     des.style.width=pecent+'%';

     des.innerHTML = parseInt(pecent)+'%'

    }

   }

  //分割文件核心部分slice

   blob=file.slice(start,end);

   fd=new FormData();

   fd.append('mof',blob);

   fd.append('test',file.name);

   //console.log(fd);

   //pending=true;

   xhr.send(fd);

  }else{

   //clearInterval(clock);

  }

 }

 function change(){

  des.style.width='0%';

 }

</script>

 </body>

</html>

file.php:

<?php 

/****

 waited

****/

//print_r($_FILES);exit;

$file = $_FILES['mof'];

$type = trim(strrchr($_POST['test'], '.'),'.');

// print_r($_POST['test']);exit;

if($file['error']==0){

 if(!file_exists('./upload/upload.'.$type)){

  if(!move_uploaded_file($file['tmp_name'],'./upload/upload.'.$type)){

   echo 'failed';

  }

 }else{

  $content=file_get_contents($file['tmp_name']);

  if (!file_put_contents('./upload/upload.'.$type, $content,FILE_APPEND)) {

   echo 'failed';

  }

 }

}else{

 echo 'failed';

}

?>

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助。

更多信息請(qǐng)查看網(wǎng)絡(luò)編程
易賢網(wǎng)手機(jī)網(wǎng)站地址:js實(shí)現(xiàn)分割上傳大文件
由于各方面情況的不斷調(diào)整與變化,易賢網(wǎng)提供的所有考試信息和咨詢(xún)回復(fù)僅供參考,敬請(qǐng)考生以權(quán)威部門(mén)公布的正式信息和咨詢(xún)?yōu)闇?zhǔn)!

2025國(guó)考·省考課程試聽(tīng)報(bào)名

  • 報(bào)班類(lèi)型
  • 姓名
  • 手機(jī)號(hào)
  • 驗(yàn)證碼
關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡(jiǎn)要咨詢(xún) | 簡(jiǎn)要咨詢(xún)須知 | 新媒體/短視頻平臺(tái) | 手機(jī)站點(diǎn) | 投訴建議
工業(yè)和信息化部備案號(hào):滇ICP備2023014141號(hào)-1 云南省教育廳備案號(hào):云教ICP備0901021 滇公網(wǎng)安備53010202001879號(hào) 人力資源服務(wù)許可證:(云)人服證字(2023)第0102001523號(hào)
云南網(wǎng)警備案專(zhuān)用圖標(biāo)
聯(lián)系電話:0871-65099533/13759567129 獲取招聘考試信息及咨詢(xún)關(guān)注公眾號(hào):hfpxwx
咨詢(xún)QQ:1093837350(9:00—18:00)版權(quán)所有:易賢網(wǎng)
云南網(wǎng)警報(bào)警專(zhuān)用圖標(biāo)