頁面寫法javascript
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>web uploader測試</title> </head> <body> <script src="https://cdn.bootcss.com/jquery/1.11.0-rc1/jquery.js"></script> <link rel="stylesheet" type="text/css" href="__PUBLIC__/webup/webuploader.css"> <script type="text/javascript" src="__PUBLIC__/webup/webuploader.js"></script> <div id="uploader-demo"> <!--用來存放item--> <div id="fileList" class="uploader-list"></div> <div id="filePicker">選擇圖片</div> <div id="upstart">開始上傳</div> <!-- 下面是上傳成功返回的圖片路徑,用戶form提交用的 --> <form action="{:U('index/shangchuan_save')}" method="post"> <div id="upok_result"></div> <input type="submit" value="save" /> </form> </div> <script> var BASE_URL = "__PUBLIC__/webup"; var uploader = WebUploader.create({ // 選完文件後,是否自動上傳。 auto: false, // swf文件路徑 swf: BASE_URL + '/Uploader.swf', // 文件接收服務端。此爲tp3框架的上傳方法請求,根據需求和框架修改 server: '{:U("index/shangchuan_up")}', // 選擇文件的按鈕。可選。 // 內部根據當前運行是建立,多是input元素,也多是flash. pick: '#filePicker', // 只容許選擇圖片文件。 accept: { title: 'Images', extensions: 'gif,jpg,jpeg,bmp,png', mimeTypes: 'image/jpg,image/jpeg,image/png' } }); // 當有文件添加進來的時候 uploader.on( 'fileQueued', function( file ) { var $li = $( '<div id="' + file.id + '" class="file-item thumbnail">' + '<span class="jieguo success">ok</span>'+ '<img class="web_up_img">' + '<div class="info">' + file.name + '</div>' + '</div>' ), $img = $li.find('img'); // $list爲容器jQuery實例 $('#fileList').append( $li ); // 建立縮略圖 // 若是爲非圖片文件,能夠不用調用此方法。 // thumbnailWidth x thumbnailHeight 爲 100 x 100 uploader.makeThumb( file, function( error, src ) { if ( error ) { $img.replaceWith('<span>不能預覽</span>'); return; } $img.attr( 'src', src ); }, 100, 100 ); }); // 文件上傳過程當中建立進度條實時顯示。 uploader.on( 'uploadProgress', function( file, percentage ) { var $li = $( '#'+file.id ), $percent = $li.find('.progress span'); // 避免重複建立 if ( !$percent.length ) { $percent = $('<p class="progress" style="background-color:red;"><span></span></p>') .appendTo( $li ) .find('span'); } $percent.css( 'width', percentage * 100 + '%' ); }); // 文件上傳成功,給item添加成功class, 用樣式標記上傳成功。 uploader.on( 'uploadSuccess', function( file,response ) { if(response.status==1){ //增長這個樣式,上傳成功的提示就顯示出來了 $( '#'+file.id ).addClass('upload-state-done'); //上傳成功存到隱藏的div裏面用於form提交 var str = "<input type='text' name='photos[]' value='" +response.msg.file.savepath +response.msg.file.savename +"' id='res_" +file.id +"' />"; // console.log(str); $('#upok_result').append(str); }else{ //上傳失敗顯示提示 $( '#'+file.id ).addClass('upload-state-done-error'); var $li = $( '#'+file.id ); $('<span class="jieguo error">上傳失敗</span>').prependTo( $li ); } }); // 文件上傳失敗,顯示上傳出錯。 uploader.on( 'uploadError', function( file ) { //上傳失敗顯示提示 $( '#'+file.id ).addClass('upload-state-done-error'); var $li = $( '#'+file.id ), $error = $li.find('span.error'); // 避免重複建立 if ( !$error.length ) { $error = $('<span class="jieguo error">上傳失敗</span>').appendTo( $li ); } }); // 完成上傳完了,成功或者失敗,先刪除進度條。 uploader.on( 'uploadComplete', function( file ) { //刪除進度條 $( '#'+file.id ).find('.progress').remove(); //增長刪除按鈕 $( '#'+file.id ).append('<span class="jieguo success" style="cursor:pointer;" onclick=\'delimg("'+file.id+'")\'>刪除</span>'); }); $("#upstart").click(function(){ uploader.upload(); }); //zll 刪除圖片 function delimg(fileid){ $("#"+fileid).remove(); $("#res_"+fileid).remove(); } </script> </body> </html>
webuploader.css 我也作了一點點修改css
.webuploader-container { position: relative; float: left; } .webuploader-element-invisible { position: absolute !important; clip: rect(1px 1px 1px 1px); /* IE6, IE7 */ clip: rect(1px,1px,1px,1px); } .webuploader-pick { position: relative; display: inline-block; cursor: pointer; background: #00b7ee; padding: 10px 15px; color: #fff; text-align: center; border-radius: 3px; overflow: hidden; } .webuploader-pick-hover { background: #00a2d4; } .webuploader-pick-disable { opacity: 0.6; pointer-events:none; } .uploader-list{ display: flex; flex-wrap: wrap; flex-direction: row; width: 300px; } .file-item{ /*height: 150px;*/ width: 100px; box-sizing: border-box; margin:5px; border: 1px solid gray; padding: 5px; box-shadow: 0 0 2px 1px grey; } .info{ color: white; background-color: rgba(0,0,0,0.5); line-height: 20px; font-size: 12px; text-align: center; margin-top: -24px; position: relative; height: 20px; } .web_up_img{ width: 100%; } #upstart{ background-color: #669584; color: white; width: 94px; height: 41px; line-height: 41px; text-align: center; border-radius: 3px; float: left; margin-left: 10px; } .jieguo{ display: none; color: white; text-align: center; font-size: 12px; height: 14px; line-height: 14px; } .success{ background-color: green; } .error{ background-color: red; } .upload-state-done .success{ display: block; } .upload-state-done-error .error{ display: block; }
tp3框架的上傳方法html
public function shangchuan_up(){ $upload = new \Think\Upload();// 實例化上傳類 $upload->maxSize = 3145728 ;// 設置附件上傳大小 $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 設置附件上傳類型 $upload->rootPath = './Uploads/'; // 設置附件上傳根目錄 $upload->savePath = ''; // 設置附件上傳(子)目錄 // 上傳文件 $info = $upload->upload(); if(!$info) {// 上傳錯誤提示錯誤信息 // $this->error($upload->getError()); $this->ajaxReturn(array('status'=>0,'msg'=>$upload->getError())); }else{// 上傳成功 $this->ajaxReturn(array('status'=>1,'msg'=>$info)); } }