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. MySql之安装配置

    1.解压Mysql.zip后,添加bin目录的环境变量 2.配置my.ini文件中的 # basedir =D:\mysql-5.6.24-winx64 # datadir = D:\mysql-5. ...

  2. CVTE实习面经

    一个月的实习都结束了,我才把这篇面经放出来...可能有记得不太清楚的地方,还请多多见谅. 第一次面试是在5月中旬. 这次面试问的主要是基础的问题吧,就是C和C++的基础问题,我记得有问到下面几个问题 ...

  3. checkbox标签已有checked=checked属性但是不显示勾选

    点击全选按钮,选中下面的列表,再次点击取消选择. 第一次的使用的方法是$("input[name=xxx]").attr('checked',true); 但是往往刷新页面第一次点 ...

  4. JS新手易错点

    写给自己 字符串换行不能直接换行,需要在行尾加换行符"\" var a = "aa bb" 是不行的 需要改成 var a="aa\ bb"

  5. vim的跨文件复制粘贴

    1.用vim打开一个文件,例如:a.cpp 2.在普通模式下,输入:":sp"(不含引号)横向切分一个窗口,或者":vsp"纵向切分一个窗口,敲入命令后,你将看 ...

  6. java 数据导入到exc ,并下载

    package com.lizi.admin.controller.platform.excel; import java.util.List;import java.util.Map; import ...

  7. 一台MySQL服务器启动多个端口

    一台MySQL服务器启动多个端口 在测试Mysql多主一从服务器,即一个从服务器多端口同步不同主库.本文记录了开启不同端口的操作. 详细步骤: 1.首先要先把my.cnf配置文件复制一份,开几个端口要 ...

  8. Unity中使用多构造函数(转)

    如果要实例化的类只有一个构造函数, 则使用方法很简单使用方法如下: 1 2 3 4 5 6 7 using (IUnityContainer container = new UnityContaine ...

  9. Android Activity生命周期

    从android api文档摘抄出来的activity生命周期图如下: Activity有如下四种状态 a.活动状态  activity处于屏幕前台,获取到了焦点可以和用户进行交互,同一时刻只有一个a ...

  10. jsp入门笔记

    jsp语法 1. declaration 由于访问serlvet只有一个,<%! int i = 0; %>   是servlet的变量,刷新时会不断增加 <% int i = 0; ...