- html页面:
- <!DOCTYPE html>
- <html class="js cssanimations">
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
- <title>Thinkphp拖拽上传文件-使用webuploader插件</title>
- <include file="Inc/css" />
- <link rel="stylesheet" href="__PUBLIC__/webuploader/xb-webuploader.css">
- <script src="__PUBLIC__/js/jquery.min.js"></script>
- <script src="__PUBLIC__/webuploader/webuploader.min.js"></script>
- </head>
- <body>
- <div class="admin-content">
- <form action="{:U('Mafull/webuploader')}" method="post" >
- <div id="upload-57c79f4938104" class="xb-uploader">
- <input type="hidden" name="image" id="image">
- <div class="queueList">
- <div class="placeholder" style="padding: 20px">
- <div class="filePicker"></div>
- </div>
- </div>
- <div class="statusBar" style="display:none;">
- <div class="progress">
- <span class="text">0%</span>
- <span class="percentage"></span>
- </div>
- <div class="info"></div>
- <!-- <div class="btns">
- <div class="uploadBtn">开始上传</div>
- </div>-->
- </div>
- </div>
- </div>
- </form>
- </div>
- <script>
- /*上传文件操作 开始*/
- jQuery(function() {
- var $ = jQuery, // just in case. Make sure it's not an other libaray.
- $wrap = $("#upload-57c79f4938104"),
- // 图片容器
- $queue = $('<ul class="filelist"></ul>')
- .appendTo( $wrap.find('.queueList') ),
- // 状态栏,包括进度和控制按钮
- $statusBar = $wrap.find('.statusBar'),
- // 文件总体选择信息。
- $info = $statusBar.find('.info'),
- // 上传按钮
- $upload = $wrap.find('.uploadBtn'),
- // 没选择文件之前的内容。
- $placeHolder = $wrap.find('.placeholder'),
- // 总体进度条
- $progress = $statusBar.find('.progress').hide(),
- // 添加的文件数量
- fileCount = 0,
- // 添加的文件总大小
- fileSize = 0,
- // 优化retina, 在retina下这个值是2
- ratio = window.devicePixelRatio || 1,
- // 缩略图大小
- thumbnailWidth = 110 * ratio,
- thumbnailHeight = 110 * ratio,
- // 可能有pedding, ready, uploading, confirm, done.
- state = 'pedding',
- // 所有文件的进度信息,key为file id
- percentages = {},
- supportTransition = (function(){
- var s = document.createElement('p').style,
- r = 'transition' in s ||
- 'WebkitTransition' in s ||
- 'MozTransition' in s ||
- 'msTransition' in s ||
- 'OTransition' in s;
- s = null;
- return r;
- })(),
- // WebUploader实例
- uploader;
- if ( !WebUploader.Uploader.support() ) {
- alert( 'Web Uploader 不支持您的浏览器!如果你使用的是IE浏览器,请尝试升级 flash 播放器');
- throw new Error( 'WebUploader does not support the browser you are using.' );
- }
- // 实例化
- uploader = WebUploader.create({
- auto: true,// 选完文件后,是否自动上传。
- compress:false,
- pick: {
- id: "#upload-57c79f4938104 .filePicker",
- label: '点击选择文件',
- multiple : true
- },
- dnd: "#upload-57c79f4938104 .queueList",
- paste: document.body,
- // swf文件路径
- swf: BASE_URL + '/Uploader.swf',
- disableGlobalDnd: true,// [可选] [默认值:false]是否禁掉整个页面的拖拽功能,如果不禁用,图片拖进来的时候会默认被浏览器打开。
- server: "__URL__/ajax_upload",
- chunked: true,//是否切片
- chunkSize:10*1024*1024,
- fileNumLimit: 1,
- fileSizeLimit: 1024 * 1024 * 1024, // 1G
- fileSingleSizeLimit: 1024 * 1024 * 1024 // 1G
- });
- // 当有文件添加进来时执行,负责view的创建
- function addFile( file ) {
- var $li = $( '<li id="' + file.id + '">' +
- '<p class="title">' + file.name + '</p>' +
- '<p class="imgWrap"></p>'+
- '<p class="progress"><span></span></p>' +
- '</li>' ),
- $btns = $('<div class="file-panel">' +
- '<span class="cancel">删除</span>' +
- '<span class="rotateRight">向右旋转</span>' +
- '<span class="rotateLeft">向左旋转</span></div>').appendTo( $li ),
- $prgress = $li.find('p.progress span'),
- $wrap = $li.find( 'p.imgWrap' ),
- $info = $('<p class="error"></p>'),
- showError = function( code ) {
- switch( code ) {
- case 'exceed_size':
- text = '文件大小超出';
- break;
- case 'interrupt':
- text = '上传暂停';
- break;
- default:
- text = '上传失败,请重试';
- break;
- }
- $info.text( text ).appendTo( $li );
- };
- if ( file.getStatus() === 'invalid' ) {
- showError( file.statusText );
- } else {
- // @todo lazyload
- $wrap.text( '预览中' );
- uploader.makeThumb( file, function( error, src ) {
- if ( error ) {
- $wrap.text( '不能预览' );
- return;
- }
- var img = $('<img src="'+src+'">');
- $wrap.empty().append( img );
- }, thumbnailWidth, thumbnailHeight );
- percentages[ file.id ] = [ file.size, 0 ];
- file.rotation = 0;
- }
- file.on('statuschange', function( cur, prev ) {
- if ( prev === 'progress' ) {
- $prgress.hide().width(0);
- } else if ( prev === 'queued' ) {
- $li.off( 'mouseenter mouseleave' );
- $btns.remove();
- }
- // 成功
- if ( cur === 'error' || cur === 'invalid' ) {
- console.log( file.statusText );
- showError( file.statusText );
- percentages[ file.id ][ 1 ] = 1;
- } else if ( cur === 'interrupt' ) {
- showError( 'interrupt' );
- } else if ( cur === 'queued' ) {
- percentages[ file.id ][ 1 ] = 0;
- } else if ( cur === 'progress' ) {
- $info.remove();
- $prgress.css('display', 'block');
- } else if ( cur === 'complete' ) {
- $li.append( '<span class="success"></span>' );
- }
- $li.removeClass( 'state-' + prev ).addClass( 'state-' + cur );
- });
- $li.on( 'mouseenter', function() {
- $btns.stop().animate({height: 30});
- });
- $li.on( 'mouseleave', function() {
- $btns.stop().animate({height: 0});
- });
- $btns.on( 'click', 'span', function() {
- var index = $(this).index(),
- deg;
- switch ( index ) {
- case 0:
- uploader.removeFile( file );
- return;
- case 1:
- file.rotation += 90;
- break;
- case 2:
- file.rotation -= 90;
- break;
- }
- if ( supportTransition ) {
- deg = 'rotate(' + file.rotation + 'deg)';
- $wrap.css({
- '-webkit-transform': deg,
- '-mos-transform': deg,
- '-o-transform': deg,
- 'transform': deg
- });
- } else {
- $wrap.css( 'filter', 'progid:DXImageTransform.Microsoft.BasicImage(rotation='+ (~~((file.rotation/90)%4 + 4)%4) +')');
- }
- });
- $li.appendTo( $queue );
- }
- // 负责view的销毁
- function removeFile( file ) {
- var $li = $('#'+file.id);
- delete percentages[ file.id ];
- updateTotalProgress();
- $li.off().find('.file-panel').off().end().remove();
- }
- function updateTotalProgress() {
- var loaded = 0,
- total = 0,
- spans = $progress.children(),
- percent;
- $.each( percentages, function( k, v ) {
- total += v[ 0 ];
- loaded += v[ 0 ] * v[ 1 ];
- } );
- percent = total ? loaded / total : 0;
- spans.eq( 0 ).text( Math.round( percent * 100 ) + '%' );
- spans.eq( 1 ).css( 'width', Math.round( percent * 100 ) + '%' );
- updateStatus();
- }
- function updateStatus() {
- var text = '', stats;
- if ( state === 'ready' ) {
- text = '选中' + fileCount + '个文件,共' +
- WebUploader.formatSize( fileSize ) + '。';
- } else if ( state === 'confirm' ) {
- stats = uploader.getStats();
- if ( stats.uploadFailNum ) {
- text = '已成功上传' + stats.successNum+ '个文件,'+
- stats.uploadFailNum + '个上传失败,<a class="retry" href="#">重新上传</a>失败文件或<a class="ignore" href="#">忽略</a>'
- }
- } else {
- stats = uploader.getStats();
- if(stats.successNum==0){
- text="";
- }
- else
- {
- text = '共' + fileCount + '个(' +
- WebUploader.formatSize( fileSize ) +
- '),已上传' + stats.successNum + '个<br /><p style="font-size:22px; color:#0b8cec;font-weight: bold;">已上传成功,点击“下一步”吧</p>';
- }
- if ( stats.uploadFailNum ) {
- text += ',失败' + stats.uploadFailNum + '个';
- }
- }
- var txtSize=WebUploader.formatSize(fileSize);
- if(txtSize=="0B"){
- $("#upload-57c79f4938104 input[name='size']").val('');
- }
- else{
- $("#upload-57c79f4938104 input[name='size']").val(txtSize);
- }
- $info.html( text );
- }
- uploader.onUploadAccept=function(object ,ret){
- if(ret.error_info){
- fileError=ret.error_info;
- return false;
- }
- }
- uploader.onUploadSuccess=function(file ,response){
- fileName=response.filePath;
- filePixels=response.filePixels;
- }
- uploader.onUploadError=function(file){
- alert(fileError);
- }
- function setState( val ) {
- var file, stats;
- if ( val === state ) {
- return;
- }
- $upload.removeClass( 'state-' + state );
- $upload.addClass( 'state-' + val );
- state = val;
- switch ( state ) {
- case 'pedding':
- $placeHolder.removeClass( 'element-invisible' );
- $queue.parent().removeClass('filled');
- $queue.hide();
- $statusBar.addClass( 'element-invisible' );
- uploader.refresh();
- break;
- case 'ready':
- $placeHolder.addClass( 'element-invisible' );
- $( "#upload-57c79f4938104 .filePicker2" ).removeClass( 'element-invisible');
- $queue.parent().addClass('filled');
- $queue.show();
- $statusBar.removeClass('element-invisible');
- uploader.refresh();
- break;
- case 'uploading':
- $( "#upload-57c79f4938104 .filePicker2" ).addClass( 'element-invisible' );
- $progress.show();
- $upload.text( '暂停上传' );
- break;
- case 'paused':
- $progress.show();
- $upload.text( '继续上传' );
- break;
- case 'confirm':
- $progress.hide();
- $upload.text( '开始上传' ).addClass( 'disabled' );
- stats = uploader.getStats();
- if ( stats.successNum && !stats.uploadFailNum ) {
- setState( 'finish' );
- return;
- }
- break;
- case 'finish':
- stats = uploader.getStats();
- if ( stats.successNum ) {
- $("#upload-57c79f4938104 input[name='image']").val(fileName);
- if(filePixels=="*px"){
- $("#upload-57c79f4938104 input[name='pixels']").val();
- }
- else
- {
- $("#upload-57c79f4938104 input[name='pixels']").val(filePixels);
- }
- } else {
- // 没有成功的图片,重设
- state = 'done';
- location.reload();
- }
- break;
- }
- updateStatus();
- }
- uploader.onUploadProgress = function( file, percentage ) {
- var $li = $('#'+file.id),
- $percent = $li.find('.progress span');
- $percent.css( 'width', percentage * 100 + '%' );
- percentages[ file.id ][ 1 ] = percentage;
- updateTotalProgress();
- };
- uploader.onFileQueued = function( file ) {
- fileCount++;
- fileSize += file.size;
- if ( fileCount === 1 ) {
- $placeHolder.addClass( 'element-invisible' );
- $statusBar.show();
- }
- addFile( file );
- setState( 'ready' );
- updateTotalProgress();
- };
- uploader.onFileDequeued = function( file ) {
- fileCount--;
- fileSize -= file.size;
- if ( !fileCount ) {
- setState( 'pedding' );
- }
- removeFile( file );
- updateTotalProgress();
- };
- uploader.on( 'all', function( type ) {
- var stats;
- switch( type ) {
- case 'uploadFinished':
- setState( 'confirm' );
- break;
- case 'startUpload':
- setState( 'uploading' );
- break;
- case 'stopUpload':
- setState( 'paused' );
- break;
- }
- });
- uploader.onError = function( code ) {
- alert( 'Eroor: ' + code );
- };
- $upload.on('click', function() {
- if ( $(this).hasClass( 'disabled' ) ) {
- return false;
- }
- if ( state === 'ready' ) {
- uploader.upload();
- } else if ( state === 'paused' ) {
- uploader.upload();
- } else if ( state === 'uploading' ) {
- uploader.stop();
- }
- });
- $info.on( 'click', '.retry', function() {
- uploader.retry();
- } );
- $info.on( 'click', '.ignore', function() {
- alert( 'todo' );
- } );
- $upload.addClass( 'state-' + state );
- updateTotalProgress();
- });
- /*上传文件操作 结束*/
- </script>
- <script>
- var BASE_URL = '__PUBLIC__/webuploader';
- </script>
- <script src="__PUBLIC__/webuploader/webuploader.min.js"></script>
- </body>
- </html>
- php:MafullController.class.php中写入上传方法:
- /**
- * webuploader 上传demo
- */
- public function webuploader(){
- // 如果是post提交则显示上传的文件 否则显示上传页面
- if(IS_POST){
- $image=I('post.image');
- // 判断是否有文件上传
- if (empty($image)) {
- die('没有上传文件');
- }
- echo '上传成功路径为:'.$image;
- }else{
- $this->display();
- }
- }
- //切片上传方法
- public function ajax_upload()
- {
- //故意写一个过期时间目的也是让浏览器去重新读取页面内容.你要知道,浏览器一般情况下去保存你访问过的页面的大部分内容,你第二次访问的时候,保存的内容(称为缓存)浏览器就不需要再向服务器请求了,这样节约时间,也减轻了服务器的负担.
- header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");//内容过期时间
- header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");//标记内容最后修改时间
- header("Cache-Control: no-store, no-cache, must-revalidate");//强制不缓存
- header("Cache-Control: post-check=0, pre-check=0", false);//Internet Explorer 5对于HTTP头信息使用两种新的时间间隔指示
- header("Pragma: no-cache");//禁止本页被缓存
- //$_SERVER['REQUEST_METHOD']这个变量表示的是表单提交数据的方式,get或者post
- if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
- exit; // 完成预检CORS请求
- }
- if ( !empty($_REQUEST[ 'debug' ]) ) {
- $random = rand(0, intval($_REQUEST[ 'debug' ]) );
- if ( $random === 0 ) {
- header("HTTP/1.0 500 Internal Server Error");
- exit;
- }
- }
- // 5分钟执行时间
- @set_time_limit(5 * 60);
- $targetDir = 'Public\Upload'.DIRECTORY_SEPARATOR.'file_material_tmp';
- $uploadDir = 'Public\Upload'.DIRECTORY_SEPARATOR.'file_material';
- $cleanupTargetDir = true; // 是否删除以前的临时文件内容
- $maxFileAge = 5 * 3600; // 临时文件时间(以秒为单位)
- // 获取文件名
- if (!file_exists($targetDir)) {
- @mkdir($targetDir);//mkdir() 函数创建目录。
- }
- // 创建目标目录
- if (!file_exists($uploadDir)) {
- @mkdir($uploadDir);//mkdir() 函数创建目录。
- }
- // 获取文件名
- if (isset($_REQUEST["name"])) {
- $fileName = $_REQUEST["name"];
- } elseif (!empty($_FILES)) {
- $fileName = $_FILES["file"]["name"];
- } else {
- $fileName = uniqid("file_");
- }
- $fileName=iconv("UTF-8", "gb2312", $fileName);
- $oldName = $fileName;
- $filePath = $targetDir . DIRECTORY_SEPARATOR . $fileName;
- // 取得chunk和chunks
- $chunk = isset($_REQUEST["chunk"]) ? intval($_REQUEST["chunk"]) : 0;
- $chunks = isset($_REQUEST["chunks"]) ? intval($_REQUEST["chunks"]) : 1;
- // 删除以前的临时文件内容,如:file_material_tmp文件夹内的文件
- if ($cleanupTargetDir) {
- if (!is_dir($targetDir) || !$dir = opendir($targetDir)) {
- //is_dir -- 判断给定文件名是否是一个目录
- //opendir()函数的作用是:打开目录句柄。
- die('{"jsonrpc" : "2.0", "error" : {"code": 100, "message": "Failed to open temp directory."}, "id" : "id"}');
- }
- while (($file = readdir($dir)) !== false) {//readdir ,readdir_r,----读一个目录
- $tmpfilePath = $targetDir . DIRECTORY_SEPARATOR . $file;
- // 如果临时文件是当前文件,继续下一步
- if ($tmpfilePath == "{$filePath}_{$chunk}.part" || $tmpfilePath == "{$filePath}_{$chunk}.parttmp") {
- continue;
- }
- // 删除临时文件,如果它早于最大年龄,并且不是当前文件
- //preg_match() 函数用于进行正则表达式匹配,成功返回 1 ,否则返回 0
- if (preg_match('/\.(part|parttmp)$/', $file) && (@filemtime($tmpfilePath) < time() - $maxFileAge)) {
- //filemtime() 函数返回文件内容上次的修改时间。若成功,则时间以 Unix 时间戳的方式返回。若失败,则返回 false。
- @unlink($tmpfilePath);//unlink() 函数删除文件。
- }
- }
- closedir($dir);
- }
- // 打开临时文件
- if (!$out = @fopen("{$filePath}_{$chunk}.parttmp", "wb")) {
- die('{"jsonrpc" : "2.0", "error" : {"code": 102, "message": "Failed to open output stream."}, "id" : "id"}');
- }
- if (!empty($_FILES)) {
- if ($_FILES["file"]["error"] || !is_uploaded_file($_FILES["file"]["tmp_name"])) {
- die('{"jsonrpc" : "2.0", "error" : {"code": 103, "message": "Failed to move uploaded file."}, "id" : "id"}');
- }
- // 读取二进制输入流并将其附加到临时文件
- if (!$in = @fopen($_FILES["file"]["tmp_name"], "rb")) {
- die('{"jsonrpc" : "2.0", "error" : {"code": 101, "message": "Failed to open input stream."}, "id" : "id"}');
- }
- } else {
- if (!$in = @fopen("php://input", "rb")) {
- die('{"jsonrpc" : "2.0", "error" : {"code": 101, "message": "Failed to open input stream."}, "id" : "id"}');
- }
- }
- while ($buff = fread($in, 4096)) {
- fwrite($out, $buff);
- }
- @fclose($out);
- @fclose($in);
- rename("{$filePath}_{$chunk}.parttmp", "{$filePath}_{$chunk}.part");
- $index = 0;
- $done = true;
- for( $index = 0; $index < $chunks; $index++ ) {
- if ( !file_exists("{$filePath}_{$index}.part") ) {
- $done = false;
- break;
- }
- }
- if ( $done ) {
- $pathInfo = pathinfo($fileName);
- $hashStr = substr(md5($pathInfo['basename']),8,16);
- $hashName = time() . $hashStr . '.' .$pathInfo['extension'];
- $uploadPath = $uploadDir . DIRECTORY_SEPARATOR .$hashName;
- if (!$out = @fopen($uploadPath, "wb")) {
- die('{"jsonrpc" : "2.0", "error" : {"code": 102, "message": "Failed to open output stream."}, "id" : "id"}');
- }
- if ( flock($out, LOCK_EX) ) {
- for( $index = 0; $index < $chunks; $index++ ) {
- if (!$in = @fopen("{$filePath}_{$index}.part", "rb")) {
- break;
- }
- while ($buff = fread($in, 4096)) {
- fwrite($out, $buff);
- }
- @fclose($in);
- @unlink("{$filePath}_{$index}.part");
- }
- flock($out, LOCK_UN);
- }
- @fclose($out);
- //$data=array();
- //$data['name']=$uploadPath;//Public\Upload\file_material\14793553561ee00a15b8a23204.jpg
- //echo json_encode($data);`
- //exit;
- //$data['name']=trim($uploadPath);
- $list = getimagesize($uploadPath);
- $data['pixels']=trim($list[0]."*".$list[1].'px');
- $response = array(
- 'success'=>true,
- 'oldName'=>$oldName,
- 'filePath'=>trim(str_replace("\\","/",substr($uploadPath,6))),
- 'fileSize'=>$data['size'],
- 'fileSuffixes'=>$pathInfo['extension'],
- 'file_id'=>$data['id'],
- 'filePixels'=>$data['pixels'],
- );
- die(json_encode($response));
- }
- // 返回成功JSON-RPC响应
- die('{"jsonrpc" : "2.0", "result" : null, "id" : "id"}');
- }