1. 簡介
Plupload是不支持斷點續傳的,但是他支持分片上傳,因此我們只需要打開其分片上傳后,在文件上傳之前請求一個服務器當前文件已上傳的大小,
如果上傳大小為0則認為沒有上傳過,重新上傳,如果上傳文件大小大於0則從已上傳的切片的下一個切片開始上傳。
Plupload分片上傳使用了html5的新特性,針對ie8 ie9等不支持html5的情況,采用flash等進行分片上傳,
但是plupload不建議采用flash分片上傳,因為flash在分片上傳讀取待上傳文件時不能讀取一部分,只能將文件全部加載到內存中再進行分片上傳,會消耗客戶端內存。
因此默認情況沒有打開分片上傳,而且根據他官方給的強制打開方法也不好使.
1 <!DOCTYPE html> 2 <html xmlns="http://www.w3.org/1999/xhtml" dir="ltr"> 3 <head> 4 <meta http-equiv="content-type" content="text/html; charset=UTF-8"/> 5 <script type="text/javascript" src="../js/plupload.full.min.js"></script> 6 </head> 7 8 <body style="font: 13px Verdana; background: #eee; color: #333"> 9 10 <h1>斷點上傳</h1> 11 <p>Shows you how to use the core plupload API.</p> 12 <div id="filelist">Your browser doesn't have Flash, Silverlight or HTML5 support.</div> 13 <br/> 14 15 <div id="container"> 16 <a id="pickfiles" href="javascript:;">[選擇文件]</a> 17 <a id="uploadfiles" href="javascript:;">[上傳文件]</a> 18 </div> 19 20 <br/> 21 <pre id="console"></pre> 22 23 <script type="text/javascript"> 24 25 var uploader = new plupload.Uploader({ 26 runtimes: 'html5,flash,silverlight,html4', 27 browse_button: 'pickfiles', // you can pass an id... 28 container: document.getElementById('container'), // ... or DOM Element itself 29 url: 'upload.php', 30 flash_swf_url: '../js/Moxie.swf', 31 silverlight_xap_url: '../js/Moxie.xap', 32 chunk_size: '2000kb', //分片上傳文件時,每片文件被切割成的大小,為數字時單位為字節 33 max_retries: 100, //當發生plupload.HTTP_ERROR
錯誤時的重試次數,為0時表示不重試 34 filters: { 35 max_file_size: '10G', 36 mime_types: [ 37 {title: "Image files", extensions: "jpg,gif,png"}, 38 {title: "Zip files", extensions: "zip"}, 39 {title: "Video Files", extensions: "dat,asf,rm,ram,3gp,mov,m4v,dvix,dv,qt,divx,cpk,fli,flc,mod,mp4,wmv,flv,avi,mkv,vob,mpg,rmvb,mpeg,mov,mts"} 40 41 ], 42 multi_selection: true, 43 }, 44 45 init: { 46 PostInit: function () { 47 document.getElementById('filelist').innerHTML = ''; 48 49 document.getElementById('uploadfiles').onclick = function () { 50 uploader.start(); 51 return false; 52 }; 53 }, 54 55 FilesAdded: function (up, files) { 56 plupload.each(files, function (file) { 57 document.getElementById('filelist').innerHTML += '<div id="' + file.id + '">' + file.name + ' (' + plupload.formatSize(file.size) + ') <b></b></div>'; 58 }); 59 }, 60 61 UploadProgress: function (up, file) { 62 document.getElementById(file.id).getElementsByTagName('b')[0].innerHTML = '<span>' + file.percent + "%</span>"; 63 }, 64 65 Error: function (up, err) { 66 document.getElementById('console').appendChild(document.createTextNode("\nError #" + err.code + ": " + err.message)); 67 } 68 } 69 }); 70 71 uploader.init(); 72 </script> 73 </body> 74 </html>
<php部分>
1 <?php 2 /** 3 * upload.php 4 * 5 * Copyright 2013, Moxiecode Systems AB 6 * Released under GPL License. 7 * 8 * License: http://www.plupload.com/license 9 * Contributing: http://www.plupload.com/contributing 10 */ 11 12 #!! IMPORTANT: 13 #!! this file is just an example, it doesn't incorporate any security checks and 14 #!! is not recommended to be used in production environment as it is. Be sure to 15 #!! revise it and customize to your needs. 16 17 18 // Make sure file is not cached (as it happens for example on iOS devices) 19 header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); 20 header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); 21 header("Cache-Control: no-store, no-cache, must-revalidate"); 22 header("Cache-Control: post-check=0, pre-check=0", false); 23 header("Pragma: no-cache"); 24 25 /* 26 // Support CORS 27 header("Access-Control-Allow-Origin: *"); 28 // other CORS headers if any... 29 if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') { 30 exit; // finish preflight CORS requests here 31 } 32 */ 33 34 // 5 minutes execution time 35 @set_time_limit(5 * 60); 36 37 // Uncomment this one to fake upload time 38 // usleep(5000); 39 40 // Settings 41 $targetDir = ini_get("upload_tmp_dir") . DIRECTORY_SEPARATOR . "plupload"; 42 //$targetDir = 'uploads'; 43 $cleanupTargetDir = true; // Remove old files 44 $maxFileAge = 5 * 3600; // Temp file age in seconds 45 46 47 error_log(var_export($_REQUEST,true),3,'/home/zhouyu/txt.log'); 48 49 // Create target dir 50 if (!file_exists($targetDir)) { 51 @mkdir($targetDir); 52 } 53 54 // Get a file name 55 if (isset($_REQUEST["name"])) { 56 $fileName = $_REQUEST["name"]; 57 } elseif (!empty($_FILES)) { 58 $fileName = $_FILES["file"]["name"]; 59 } else { 60 $fileName = uniqid("file_"); 61 } 62 63 $filePath = $targetDir . DIRECTORY_SEPARATOR . $fileName; 64 65 // Chunking might be enabled 66 $chunk = isset($_REQUEST["chunk"]) ? intval($_REQUEST["chunk"]) : 0; 67 $chunks = isset($_REQUEST["chunks"]) ? intval($_REQUEST["chunks"]) : 0; 68 69 70 // Remove old temp files 71 if ($cleanupTargetDir) { 72 if (!is_dir($targetDir) || !$dir = opendir($targetDir)) { 73 die('{"jsonrpc" : "2.0", "error" : {"code": 100, "message": "Failed to open temp directory."}, "id" : "id"}'); 74 } 75 76 while (($file = readdir($dir)) !== false) { 77 $tmpfilePath = $targetDir . DIRECTORY_SEPARATOR . $file; 78 79 // If temp file is current file proceed to the next 80 if ($tmpfilePath == "{$filePath}.part") { 81 continue; 82 } 83 84 // Remove temp file if it is older than the max age and is not the current file 85 if (preg_match('/\.part$/', $file) && (filemtime($tmpfilePath) < time() - $maxFileAge)) { 86 @unlink($tmpfilePath); 87 } 88 } 89 closedir($dir); 90 } 91 92 93 // Open temp file 94 if (!$out = @fopen("{$filePath}.part", $chunks ? "ab" : "wb")) { 95 die('{"jsonrpc" : "2.0", "error" : {"code": 102, "message": "Failed to open output stream."}, "id" : "id"}'); 96 } 97 98 if (!empty($_FILES)) { 99 if ($_FILES["file"]["error"] || !is_uploaded_file($_FILES["file"]["tmp_name"])) { 100 die('{"jsonrpc" : "2.0", "error" : {"code": 103, "message": "Failed to move uploaded file."}, "id" : "id"}'); 101 } 102 103 // Read binary input stream and append it to temp file 104 if (!$in = @fopen($_FILES["file"]["tmp_name"], "rb")) { 105 die('{"jsonrpc" : "2.0", "error" : {"code": 101, "message": "Failed to open input stream."}, "id" : "id"}'); 106 } 107 } else { 108 if (!$in = @fopen("php://input", "rb")) { 109 die('{"jsonrpc" : "2.0", "error" : {"code": 101, "message": "Failed to open input stream."}, "id" : "id"}'); 110 } 111 } 112 113 while ($buff = fread($in, 4096)) { 114 fwrite($out, $buff); 115 } 116 117 @fclose($out); 118 @fclose($in); 119 120 // Check if file has been uploaded 121 if (!$chunks || $chunk == $chunks - 1) { 122 // Strip the temp .part suffix off 123 rename("{$filePath}.part", $filePath); 124 } 125 126 // Return Success JSON-RPC response 127 die('{"jsonrpc" : "2.0", "result" : null, "id" : "id"}');