http://www.resumablejs.com/ 官网
upload.html
<!DOCTYPE html>
<html lang="en"> <div>
<a href="#" id="browseButton" >Select files</a>
<div>
<div>
<input id="btnCancel" type="button" onClick='r.pause()'value="Cancel All Uploads"
style="margin-left: 2px; height: 22px; font-size: 8pt;" />
<br />
</div>
<script src="resumable.js"></script>
<script>
var r = new Resumable({
target:'upload.php',
chunkSize:**,
simultaneousUploads:,
testChunks:true,
throttleProgressCallbacks:, }); r.assignBrowse(document.getElementById('browseButton')); r.on('fileSuccess', function(file){
// console.debug(file);
});
r.on('fileProgress', function(file){
// console.debug(file);
});
r.on('fileAdded', function(file, event){
r.upload();
//console.debug(file, event);
});
r.on('fileRetry', function(file){
//console.debug(file);
});
r.on('fileError', function(file, message){
//console.debug(file, message);
});
r.on('uploadStart', function(){
//console.debug();
});
r.on('complete', function(){
//console.debug();
});
r.on('progress', function(){
//console.debug();
});
r.on('error', function(message, file){
//console.debug(message, file);
});
r.on('pause', function(file,message){
//console.debug(); });
r.on('cancel', function(){
//console.debug();
});
</script> </html> upload.php
<?php
/**
* This is the implementation of the server side part of
* Resumable.js client script, which sends/uploads files
* to a server in several chunks.
*
* The script receives the files in a standard way as if
* the files were uploaded using standard HTML form (multipart).
*
* This PHP script stores all the chunks of a file in a temporary
* directory (`temp`) with the extension `_part<#ChunkN>`. Once all
* the parts have been uploaded, a final destination file is
* being created from all the stored parts (appending one by one).
*
* @author Gregory Chris (http://online-php.com)
* @email www.online.php@gmail.com
*/ ////////////////////////////////////////////////////////////////////
// THE FUNCTIONS
//////////////////////////////////////////////////////////////////// /**
*
* Logging operation - to a file (upload_log.txt) and to the stdout
* @param string $str - the logging string
*/
function _log($str) { // log to the output
$log_str = date('d.m.Y').": {$str}\r\n";
echo $log_str; // log to file
if (($fp = fopen('upload_log.txt', 'a+')) !== false) {
fputs($fp, $log_str);
fclose($fp);
}
} /**
*
* Delete a directory RECURSIVELY
* @param string $dir - directory path
* @link http://php.net/manual/en/function.rmdir.php
*/
function rrmdir($dir) {
if (is_dir($dir)) {
$objects = scandir($dir);
foreach ($objects as $object) {
if ($object != "." && $object != "..") {
if (filetype($dir . "/" . $object) == "dir") {
rrmdir($dir . "/" . $object);
} else {
unlink($dir . "/" . $object);
}
}
}
reset($objects);
rmdir($dir);
}
} /**
*
* Check if all the parts exist, and
* gather all the parts of the file together
* @param string $dir - the temporary directory holding all the parts of the file
* @param string $fileName - the original file name
* @param string $chunkSize - each chunk size (in bytes)
* @param string $totalSize - original file size (in bytes)
*/
function createFileFromChunks($temp_dir, $fileName, $chunkSize, $totalSize) { // count all the parts of this file
$total_files = ;
foreach(scandir($temp_dir) as $file) {
if (stripos($file, $fileName) !== false) {
$total_files++;
}
} // check that all the parts are present
// the size of the last part is between chunkSize and 2*$chunkSize
if ($total_files * $chunkSize >= ($totalSize - $chunkSize + )) { // create the final destination file
if (($fp = fopen('temp/'.$fileName, 'w')) !== false) {
for ($i=; $i<=$total_files; $i++) {
fwrite($fp, file_get_contents($temp_dir.'/'.$fileName.'.part'.$i));
_log('writing chunk '.$i);
}
fclose($fp);
} else {
_log('cannot create the destination file');
return false;
} // rename the temporary directory (to avoid access from other
// concurrent chunks uploads) and than delete it
if (rename($temp_dir, $temp_dir.'_UNUSED')) {
rrmdir($temp_dir.'_UNUSED');
} else {
rrmdir($temp_dir);
}
} } ////////////////////////////////////////////////////////////////////
// THE SCRIPT
//////////////////////////////////////////////////////////////////// //check if request is GET and the requested chunk exists or not. this makes testChunks work
if ($_SERVER['REQUEST_METHOD'] === 'GET') { $temp_dir = 'temp/'.$_GET['resumableIdentifier'];
$chunk_file = $temp_dir.'/'.$_GET['resumableFilename'].'.part'.$_GET['resumableChunkNumber'];
if (file_exists($chunk_file)) {
header("HTTP/1.0 200 Ok");
} else
{
header("HTTP/1.0 404 Not Found");
}
} // loop through files and move the chunks to a temporarily created directory
if (!empty($_FILES)) foreach ($_FILES as $file) { // check the error status
if ($file['error'] != ) {
_log('error '.$file['error'].' in file '.$_POST['resumableFilename']);
continue;
} // init the destination file (format <filename.ext>.part<#chunk>
// the file is stored in a temporary directory
$temp_dir = 'temp/'.$_POST['resumableIdentifier'];
$dest_file = $temp_dir.'/'.$_POST['resumableFilename'].'.part'.$_POST['resumableChunkNumber']; // create the temporary directory
if (!is_dir($temp_dir)) {
mkdir($temp_dir, , true);
} // move the temporary file
if (!move_uploaded_file($file['tmp_name'], $dest_file)) {
_log('Error saving (move_uploaded_file) chunk '.$_POST['resumableChunkNumber'].' for file '.$_POST['resumableFilename']);
} else { // check if all the parts present, and create the final destination file
createFileFromChunks($temp_dir, $_POST['resumableFilename'],
$_POST['resumableChunkSize'], $_POST['resumableTotalSize']);
}
}

resumablejs 分块上传 断点续传的更多相关文章

  1. php大文件分块上传断点续传demo

    前段时间做视频上传业务,通过网页上传视频到服务器. 视频大小 小则几十M,大则 1G+,以一般的HTTP请求发送数据的方式的话,会遇到的问题:1,文件过大,超出服务端的请求大小限制:2,请求时间过长, ...

  2. web大文件分块上传断点续传demo

    一.概述 所谓断点续传,其实只是指下载,也就是要从文件已经下载的地方开始继续下载.在以前版本的HTTP协议是不支持断点的,HTTP/1.1开始就支持了.一般断点下载时才用到Range和Content- ...

  3. asp.net大文件分块上传断点续传demo

    IE的自带下载功能中没有断点续传功能,要实现断点续传功能,需要用到HTTP协议中鲜为人知的几个响应头和请求头. 一. 两个必要响应头Accept-Ranges.ETag 客户端每次提交下载请求时,服务 ...

  4. .net大文件分块上传断点续传demo

    IE的自带下载功能中没有断点续传功能,要实现断点续传功能,需要用到HTTP协议中鲜为人知的几个响应头和请求头. 一. 两个必要响应头Accept-Ranges.ETag 客户端每次提交下载请求时,服务 ...

  5. js大文件分块上传断点续传demo

    文件夹上传:从前端到后端 文件上传是 Web 开发肯定会碰到的问题,而文件夹上传则更加难缠.网上关于文件夹上传的资料多集中在前端,缺少对于后端的关注,然后讲某个后端框架文件上传的文章又不会涉及文件夹. ...

  6. java大文件分块上传断点续传demo

    第一点:Java代码实现文件上传 FormFile file = manform.getFile(); String newfileName = null; String newpathname =  ...

  7. 前端js怎么实现大文件G级的断点续传(分块上传)和分段下载

    需求: 支持大文件批量上传(20G)和下载,同时需要保证上传期间用户电脑不出现卡死等体验: 内网百兆网络上传速度为12MB/S 服务器内存占用低 支持文件夹上传,文件夹中的文件数量达到1万个以上,且包 ...

  8. Asp.net mvc 大文件上传 断点续传

    Asp.net mvc 大文件上传 断点续传 进度条   概述 项目中需要一个上传200M-500M的文件大小的功能,需要断点续传.上传性能稳定.突破asp.net上传限制.一开始看到51CTO上的这 ...

  9. Asp.net mvc 大文件上传 断点续传 进度条

    概述 项目中需要一个上传200M-500M的文件大小的功能,需要断点续传.上传性能稳定.突破asp.net上传限制.一开始看到51CTO上的这篇文章,此方法确实很不错,能够稳定的上传大文件,http: ...

随机推荐

  1. python的正负无穷float("inf")的用法

    今天,在看书的时候看到这么一个例子: 这是用来求解 从某个数字列表中找出俩个彼此最接近但是不相等的数(俩者之间的绝对差是最小的): >>> from random import ra ...

  2. java面试中问题

    HashMap数据结构 http://blog.csdn.net/weiyouyin/article/details/5693496 HashMap冲突 http://www.blogjava.net ...

  3. 拼sql条件时判断 是不是当前时间是不是周五,如果今天不是周五,就选上周五

    if (Request.QueryString["start"] == null) { for (int i = 0; i < 6; i++) { if (DateTime. ...

  4. HDU Game Theory

    5795 || 3032 把x个石子的堆分成非空两(i, j)或三堆(i, j, k)的操作->(sg[i] ^ sg[j])或(sg[i] ^ sg[j] ^ sg[k])是x的后继 #def ...

  5. liunx 防火墙开放端口的设置

    今天在liunx 服务器上遇到一个问题,tomcat服务启动后怎么也访问不到项目,找了好久的原因,终于发现原来是liunx服务防火墙限制服务端口的访问,也就不多说了,看下面解决方法. 1.查看防火墙的 ...

  6. Rational.Rose.Enterprise.v7.0 (2007)安装分享

    很多人都在找rational软件,很多都是2003的,有的宣称是2007,但结果还是2003.也许真的不存在Rational.Rose 2007,不过有IBM.Rational.Rose.Enterp ...

  7. centos 7安装配置bind

    环境: 三台虚拟机,全部安装Centos 7, tony4.hadoop.com 192.168.31.223 tony5.hadoop.com 192.168.31.224 tony6.hadoop ...

  8. UI进阶

    @import url(http://i.cnblogs.com/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/c ...

  9. QT

    http://www.cnblogs.com/csulennon/p/4483711.html

  10. HTML5中id可以用数字开头,但在css中不能正常使用

    昨晚在看<响应式Web设计:html5和css3实战>时,书中提到“HTML5中的ID指可以用数字开头”.这个还真不知道,于是测试了一下,发现了问题. 在H5描述中是这样说的: 在css样 ...