小五六资源网,小五六博客
  • 首页
  • Java
  • 原生ajax分片上传文件demo,js分片上传文件

原生ajax分片上传文件demo,js分片上传文件

发布:小五六资源网 2020年12月22日 10:29 星期二分类: Java

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <style> #jdt { width: 300px; height: 25px;
        } progress { display: none; width: 200px; height: 25px; float: left;
        } #sd { float: left;
        } </style> <script type="text/javascript"         src="https://dss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/js/lib/jquery-1-edb203c114.10.2.js"></script> <script> function bfbs(bfb, size) {

            bfb = Math.ceil(100 * bfb / size);
            bfb > 100 ? bfb = 100 : bfb = bfb; var pro = document.getElementsByTagName('progress')[0]; pro.style.display = 'block'; pro.setAttribute('value', bfb); var sd = document.getElementById('sd'); sd.innerHTML = bfb + '%';
        } const LENGTH = 2 * 1024 * 1024; //每次切割10M       </script> </head> <body> <div id="jdt"><progress max="100" value=""></progress><span id="sd"><span></div> <input type="file" name="part"> <button id="upload_file">upload</button> <div id="desc"></div> </body> <script> $(function () { var call_ajax = (formData, funcall) => { $.ajax({ type: "POST", url: "./upfile.php", data: formData, processData: false, contentType: false, dataType: "json", success: funcall
            });
        } $("#upload_file").on('click', function () { var up_file = document.getElementsByTagName('input')[0].files[0]; var formData = new FormData(); if (typeof up_file == "undefined") { return;
            } var filejson = JSON.stringify({ 'name': up_file.name, 'type': up_file.type, 'size': up_file.size,
            }); formData.append("file", filejson); var backCall = (d) => { if (d.ok == "done") { bfbs(up_file.size, up_file.size) return;
                } uploadSome(d.size, up_file)
            }; var uploadSome = (size, file) => { var sta = size; var end = sta + LENGTH; var fd = new FormData(); blob = file.slice(sta, end); fd.append("file", filejson); fd.append('part', blob); bfbs(end, file.size) call_ajax(fd, backCall);
            } call_ajax(formData, backCall);

        });

    }); </script> </html> 
	


<?php  const dist_dir = "/tmp/tx-upfiles";  $post_file  = json_decode($_POST['file'], true);  $file   = dist_dir . '/' . $post_file['name'];   if(file_exists($file)) {     $file_size  = filesize($file);     if($file_size >= $post_file['size'])     {         echo json_encode([ 'ok' => 'done' ]);         return;     }      if(empty($_FILES['part']['tmp_name']))     {         echo json_encode(['ok' => 'go', 'size' => $file_size ]);         return;     }      file_put_contents($file, file_get_contents($_FILES['part']['tmp_name']), FILE_APPEND);      $file_size  = filesize($file) + filesize( $_FILES['part']['tmp_name'] );      if( $file_size >= $post_file['size'])     {         echo json_encode([ 'ok' => 'done' ]);         return;     }  } else {     touch($file);     $file_size  = 0; }  echo json_encode(['ok' => 'go', 'size' => $file_size ]);  






温馨提示如有转载或引用以上内容之必要,敬请将本文链接作为出处标注,谢谢合作!

发表评论: