当前位置:Gxlcms > PHP教程 > html5解决大文件断点续传

html5解决大文件断点续传

时间:2021-07-01 10:21:17 帮助过:7人阅读

js代码

[html] view plain copy html5解决大文件断点续传 html5解决大文件断点续传

  1. >
  2. <html>
  3. <head>
  4. <meta charset="UTF-8https://www.gxlcms.com/"/>
  5. <title>xhr2title>
  6. head>
  7. <body>
  8. <div id="drop_areahttps://www.gxlcms.com/" style="border:3px dashed silver;width:200px; height:200pxhttps://www.gxlcms.com/">
  9. 将图片拖拽到此
  10. div>
  11. <progress value="0https://www.gxlcms.com/" max="10https://www.gxlcms.com/" id="prouploadfilehttps://www.gxlcms.com/">progress>
  12. <span id="persenthttps://www.gxlcms.com/">0%span>
  13. <br />
  14. <button onclick="stopup()https://www.gxlcms.com/" id="stophttps://www.gxlcms.com/">上传button>
  15. <script>
  16. //拖拽上传开始
  17. //-1.禁止浏览器打开文件行为
  18. document.addEventListener("drop",function(e){ //拖离
  19. e.preventDefault();
  20. })
  21. document.addEventListener("dragleave",function(e){ //拖后放
  22. e.preventDefault();
  23. })
  24. document.addEventListener("dragenter",function(e){ //拖进
  25. e.preventDefault();
  26. })
  27. document.addEventListener("dragover",function(e){ //拖来拖去
  28. e.preventDefault();
  29. })
  30. //上传进度
  31. var pro = document.getElementById('prouploadfile');
  32. var persent = document.getElementById('persent');
  33. function clearpro(){
  34. pro.value=0;
  35. persent.innerHTML="0%https://www.gxlcms.com/";
  36. }
  37. //2.拖拽
  38. var stopbutton = document.getElementById('stop');
  39. var resultfile="https://www.gxlcms.com/"
  40. var box = document.getElementById('drop_area'); //拖拽区域
  41. box.addEventListener("drop",function(e){
  42. var fileList = e.dataTransfer.files; //获取文件对象
  43. console.log(fileList)
  44. //检测是否是拖拽文件到页面的操作
  45. if(fileList.length == 0){
  46. return false;
  47. }
  48. //拖拉图片到浏览器,可以实现预览功能
  49. //规定视频格式
  50. //in_array
  51. Array.prototype.S=String.fromCharCode(2);
  52. Array.prototype.in_array=function(e){
  53. var r=new RegExp(this.S+e+this.S);
  54. return (r.test(this.S+this.join(this.S)+this.S));
  55. };
  56. var video_type=["video/mp4","video/ogg"];
  57. //创建一个url连接,供src属性引用
  58. var fileurl = window.URL.createObjectURL(fileList[0]);
  59. if(fileList[0].type.indexOf('image') === 0){ //如果是图片
  60. var str=">";
  61. document.getElementById('drop_area').innerHTML=str;
  62. }else if(video_type.in_array(fileList[0].type)){ //如果是规定格式内的视频
  63. var str="
  64. document.getElementById('drop_area').innerHTML=str;
  65. }else{ //其他格式,输出文件名
  66. //alert("不预览");
  67. var str="文件名字:https://www.gxlcms.com/"+fileList[0].name;
  68. document.getElementById('drop_area').innerHTML=str;
  69. }
  70. resultfile = fileList[0];
  71. console.log(resultfile);
  72. //切片计算
  73. filesize= resultfile.size;
  74. setsize=500*1024;
  75. filecount = filesize/setsize;
  76. //console.log(filecount)
  77. //定义进度条
  78. pro.max=parseInt(Math.ceil(filecount));
  79. i =getCookie(resultfile.name);
  80. i = (i!=null && i!="")?parseInt(i):0
  81. if(Math.floor(filecount)<i){
  82. alert("已经完成");
  83. pro.value=i+1;
  84. persent.innerHTML="100%https://www.gxlcms.com/";
  85. }else{
  86. alert(i);
  87. pro.value=i;
  88. p=parseInt(i)*100/Math.ceil(filecount)
  89. persent.innerHTML=parseInt(p)+"%";
  90. }
  91. },false);
  92. //3.ajax上传
  93. var stop=1;
  94. function xhr2(){
  95. if(stop==1){
  96. return false;
  97. }
  98. if(resultfile==""){
  99. alert("请选择文件")
  100. return false;
  101. }
  102. i=getCookie(resultfile.name);
  103. console.log(i)
  104. i = (i!=null && i!="")?parseInt(i):0
  105. if(Math.floor(filecount)<parseInt(i)){
  106. alert("已经完成");
  107. return false;
  108. }else{
  109. //alert(i)
  110. }
  111. var xhr = new XMLHttpRequest();//第一步
  112. //新建一个FormData对象
  113. var formData = new FormData(); //++++++++++
  114. //追加文件数据
  115. //改变进度条
  116. pro.value=i+1;
  117. p=parseInt(i+1)*100/Math.ceil(filecount)
  118. persent.innerHTML=parseInt(p)+"%";
  119. //进度条
  120. if((filesize-i*setsize)>setsize){
  121. blobfile= resultfile.slice(i*setsize,(i+1)*setsize);
  122. }else{
  123. blobfile= resultfile.slice(i*setsize,filesize);
  124. formData.append('lastone', Math.floor(filecount));
  125. }
  126. formData.append('file', blobfile);
  127. //return false;
  128. formData.append('blobname', i); //++++++++++
  129.    formData.append('filename', resultfile.name); //++++++++++
  130. //post方式
  131. xhr.open('POST', 'xhr2.php'); //第二步骤
  132. //发送请求
  133. xhr.send(formData); //第三步骤
  134. stopbutton.innerHTML = "暂停https://www.gxlcms.com/"
  135. //ajax返回
  136. xhr.onreadystatechange = function(){ //第四步
  137.     if ( xhr.readyState == 4 && xhr.status == 200 ) {
  138.       console.log( xhr.responseText );
  139. if(i<filecount){
  140. xhr2();
  141. }else{
  142. i=0;
  143. }
  144.     }
  145.   };
  146. //设置超时时间
  147. xhr.timeout = 20000;
  148. xhr.ontimeout = function(event){
  149.     alert('请求超时,网络拥堵!低于25K/s');
  150.   }
  151. i=i+1;
  152. setCookie(resultfile.name,i,365)
  153. }
  154. //设置cookie
  155. function setCookie(c_name,value,expiredays)
  156. {
  157. var exdate=new Date()
  158. exdate.setDate(exdate.getDate()+expiredays)
  159. document.cookie=c_name+ "=" +escape(value)+
  160. ((expiredays==null) ? "" : ";expires="+exdate.toGMTString()+https://www.gxlcms.com/";path=/")
  161. }
  162. //获取cookie
  163. function getCookie(c_name)
  164. {
  165. if (document.cookie.length>0)
  166. {
  167. c_start=document.cookie.indexOf(c_name + "=")
  168. if (c_start!=-1)
  169. {
  170. c_start=c_start + c_name.length+1
  171. c_end=document.cookie.indexOf(";",c_start)
  172. if (c_end==-1) c_end=document.cookie.length
  173. return unescape(document.cookie.substring(c_start,c_end))
  174. }
  175. }
  176. return ""
  177. }
  178. function stopup(){
  179. if(stop==1){
  180. stop = 0
  181. xhr2();
  182. }else{
  183. stop = 1
  184. stopbutton.innerHTML = "继续https://www.gxlcms.com/"
  185. }
  186. }
  187. script>
  188. body>
  189. html>


php代码

[php] view plain copy html5解决大文件断点续传 html5解决大文件断点续传

  1. //$name=$_POST['username'];
  2. $dir=$_POST['filename'];
  3. $dir="uploads/https://www.gxlcms.com/".md5($dir);
  4. file_exists($dir) or mkdir($dir,0777,true);
  5. $path=$dir."/https://www.gxlcms.com/".$_POST['blobname'];
  6. //print_r($_FILES["file"]);
  7. move_uploaded_file($_FILES["filehttps://www.gxlcms.com/"]["tmp_namehttps://www.gxlcms.com/"],$path);
  8. if(isset($_POST['lastone'])){
  9. echo $_POST['lastone'];
  10. $count=$_POST['lastone'];
  11. $fp = fopen($_POST['filename'],"abwhttps://www.gxlcms.com/");
  12. for($i=0;$i<=$count;$i++){
  13. $handle = fopen($dir."/https://www.gxlcms.com/".$i,"rbhttps://www.gxlcms.com/");
  14. fwrite($fp,fread($handle,filesize($dir."/https://www.gxlcms.com/".$i)));
  15. fclose($handle);
  16. }
  17. fclose($fp);
  18. }
  19. ?>

以上就介绍了 html5解决大文件断点续传,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

人气教程排行