1.新建上传页面
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <!--引入CSS--> <link rel="stylesheet" type="text/css" href="__ROOT__/public/webuploader/webuploader.css"> <title>Document</title> </head> <body> <div id="uploader" class="wu-example"> <!--用来存放文件信息--> <div id="thelist" class="uploader-list"></div> <div class="btns"> <div id="picker">选择文件</div> <button id="ctlBtn" class="btn btn-default">开始上传</button> </div> </div> </body> </html> <!--引入JS--> <script src="https://code.jquery.com/jquery-3.4.1.min.js"></script> <script type="text/javascript" src="__ROOT__/public/webuploader/webuploader.min.js"></script> <script> let uploader = new WebUploader.Uploader({ // swf文件路径 swf: ‘__ROOT__/public/webuploader/Uploader.swf‘, // 文件接收服务端。 server: ‘__ROOT__/public/upload.php‘, // 选择文件的按钮。可选。 // 内部根据当前运行是创建,可能是input元素,也可能是flash. pick: ‘#picker‘, // 分片上传。 chunked: true, chunkSize: 2 * 1024 * 1024, // 不压缩image, 默认如果是jpeg,文件上传前会压缩一把再上传! resize: false }); let $list = $(‘#thelist‘); // 当有文件被添加进队列的时候 uploader.on( ‘fileQueued‘, function( file ) { $list.append( ‘<div id="‘ + file.id + ‘" class="item">‘ + ‘<h4 class="info">‘ + file.name + ‘</h4>‘ + ‘<p class="state">等待上传...</p>‘ + ‘</div>‘ ); }); // 文件上传过程中创建进度条实时显示。 uploader.on( ‘uploadProgress‘, function( file, percentage ) { let $li = $( ‘#‘+file.id ), $percent = $li.find(‘.progress .progress-bar‘); // 避免重复创建 if ( !$percent.length ) { $percent = $(‘<div class="progress progress-striped active">‘ + ‘<div class="progress-bar" role="progressbar" style="width: 0%">‘ + ‘</div>‘ + ‘</div>‘).appendTo( $li ).find(‘.progress-bar‘); } $li.find(‘p.state‘).text(‘上传中‘); $percent.css( ‘width‘, percentage * 100 + ‘%‘ ); }); //开始上传 $(‘#ctlBtn‘).click(function () { uploader.upload(); }); uploader.on( ‘uploadSuccess‘, function( file, response ) { if (response.status) { $( ‘#‘+file.id ).find(‘p.state‘).text(‘已上传‘); } else { $( ‘#‘+file.id ).find(‘p.state‘).text(response.msg); } }); uploader.on( ‘uploadError‘, function( file ) { $( ‘#‘+file.id ).find(‘p.state‘).text(‘上传出错‘); }); uploader.on( ‘uploadComplete‘, function( file ) { $( ‘#‘+file.id ).find(‘.progress‘).fadeOut(); }); </script>
2.下载 webuploader插件
3.服务器 处理上传文件
<?php //储存分片 服务器目录 $tmp_dir = ‘./upload/tmp_chunk/‘ . $_REQUEST[‘name‘] . ‘/‘; if (! is_dir($tmp_dir)) { mkdir($tmp_dir, 0777, true); } move_uploaded_file($_FILES[‘file‘][‘tmp_name‘], $tmp_dir . $_REQUEST[‘chunk‘]); //合并分片 结果存储目录 if ($_REQUEST[‘chunk‘] == $_REQUEST[‘chunks‘] - 1) { //最后一个分片 $target_file = ‘./upload/results/‘; if (! is_dir($target_file)) { mkdir($target_file, 0777, true); } $target_file .= $_REQUEST[‘name‘]; $handle = fopen($target_file, ‘x+‘); for ($i=0; $i<$_REQUEST[‘chunks‘]; $i++) { fwrite($handle, file_get_contents($tmp_dir . $i)); } fclose($handle); } $msg = [ ‘$_FILES‘ => json_encode($_FILES), ‘$_REQUEST‘ => json_encode($_REQUEST), ]; echo json_encode([‘status‘ => 1, ‘msg‘ => json_encode($msg)]);
4.打开页面 上传文件 测试。