thinkphpwebuploader异步预览上传

    xiaoxiao2021-03-25  56

    今个要说的是文件上传组件:webuploader

    先来简单说几个优点:以html5为主并在低版本的浏览器中支持flash模式;

    兼容IE6+,iOS 6+, android 4+、分片并发、预览压缩、拖拽上传、md5秒传等等;

    官网:http://fex.baidu.com/webuploader/

    问答:https://github.com/fex-team/webuploader/issues

    官网的文档写的很清楚了;自己都可以根据文档整合了;

    这里之所以要写一篇博客是因为之前在thinkphp中深度整合了webuploader;

    使用起来那是相当的简单方便;一次整合终身受益;

    而且又不断的看到有童鞋们在整合的过程中碰壁;

    这里就把整合好的代码分享出来以供参考;

    示例项目:https://github.com/baijunyao/thinkphp-bjyadmin

    一:导入webuploader

    /Public/statics/webuploader-0.1.5

    印象中是做了些什么修改的;但是年代久远;没有记录了;

    建议直接复制项目中的这个文件夹;

    二:添加自定义标签

    /Application/Common/Tag/My.class.php

      protected $tags=array(         'webuploadercss'=>array('attr'=>'','close'=>0),         'webuploader'=>array('attr'=>'name,url,word','close'=>0),         'webuploaderjs'=>array('attr'=>'','close'=>0),         );     // webuploader css jquery jquery jquery     public function _webuploadercss(){         $str= <<< php < link  rel = "stylesheet"  href = "__PUBLIC__/statics/webuploader-0.1.5/xb-webuploader.css" > < script  src = "//libs.useso.com/js/jquery/1.10.2/jquery.min.js" > </ script > php;         return $str;     }       // webuploader js     public function _webuploaderjs(){         $str= <<< php < script >     var BASE_URL = '__PUBLIC__/statics/webuploader-0.1.5'; </ script > < script  src = "//cdn.staticfile.org/webuploader/0.1.5/webuploader.min.js" > </ script > php;         return $str;     }       /**      *       * @param string $tag        * url          * name name         * word      */     public function _webuploader($tag){         $url=isset($tag['url'])?$tag['url']:U('Home/Index/ajax_upload');         $name=isset($tag['name'])?$tag['name']:'file_name';         $word=isset($tag['word'])?$tag['word']:' 300 ';         $id_name='upload-'.uniqid();              $str= <<< php < div  id = "$id_name"  class = "xb-uploader" >      < div  class = "queueList" >          < div  class = "placeholder" >               < div  class = "filePicker" > </ div >               < p >$word </ p >          </ 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 = "webuploader-container filePicker2" >                    < div  class = "webuploader-pick" > </ div >                    < div  style = "position: absolute; top: 0px; left: 0px; width: 1px; height: 1px; overflow: hidden;"  id = "rt_rt_1armv2159g1o1i9c2a313hadij6" >                    </ div >               </ div >               < div  class = "uploadBtn" > </ div >          </ div >      </ div > </ div > < script > jQuery ( function ( )  {      var \ $  =  jQuery ,     // just in case. Make sure it's not an other libaray.           \ $wrap  = \ $ ( "#$id_name" ) ,            //          \ $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, retina2          ratio  =  window . devicePixelRatio  ||  1 ,            //           thumbnailWidth  =  110  *  ratio ,          thumbnailHeight  =  110  *  ratio ,            // pedding, ready, uploading, confirm, done.          state  =  'pedding' ,            // keyfile 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 ;          }) ( ) ,          thisSuccess ,          // 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 ({          pick :  {               id :  "#$id_name .filePicker" ,               label :  '' ,               multiple  :  true          } ,          dnd :  "#$id_name .queueList" ,          paste :  document . body ,          // accept: {          //     title: 'Images',          //     extensions: 'gif,jpg,jpeg,bmp,png',          //     mimeTypes: 'image/*'          // },            // swf          swf :  BASE_URL  +  '/Uploader.swf' ,            disableGlobalDnd :  true ,            chunked :  true ,          server :  "$url" ,          fileNumLimit :  300 ,          fileSizeLimit :  200  *  1024  *  1024 ,     // 200 M          fileSingleSizeLimit :  50  *  1024  *  1024     // 50 M      }) ;        //       uploader . addButton ({         id :  "#$id_name .filePicker2" ,         label :  ''      }) ;        // 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>'  +                    '<input class="bjy-filename" type="hidden" name="{$name}[]">' +                    '</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'  )  {                    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 )  + ')' ) ;                    // use jquery animate to rotation                    // \$({                    //     rotation: rotation                    // }).animate({                    //     rotation: file.rotation                    // }, {                    //     easing: 'linear',                    //     step: function( now ) {                    //         now = now * Math.PI / 180;                      //         var cos = Math.cos( now ),                    //             sin = Math.sin( now );                      //         \$wrap.css( 'filter', "progid:DXImageTransform.Microsoft.Matrix(M11=" + cos + ",M12=" + (-sin) + ",M21=" + sin + ",M22=" + cos + ",SizingMethod='auto expand')");                    //     }                    // });               }              }) ;           \ $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 ( ) ;               text  =  ''  +  fileCount  +  ''  +                         WebUploader . formatSize (  fileSize  )   +                         ''  +  stats . successNum  +  '' ;                 if  (  stats . uploadFailNum  )  {                    text  +=  ''  +  stats . uploadFailNum  +  '' ;               }               if  ( fileCount == stats . successNum  &&  stats . successNum != 0 )  {                    $ ( '#$id_name .webuploader-element-invisible' ) . remove ( ) ;               }          }           \ $info . html (  text  ) ;      }        uploader . onUploadAccept = function ( object  , ret ) {          if ( ret . error_info ) {               fileError = ret . error_info ;               return  false ;          }      }        uploader . onUploadSuccess = function ( file  , response ) {         \ $ ( '#' + file . id  + ' .bjy-filename' ) . val ( response . name )      }      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'  ) ;                   \ $ (  "#$id_name .filePicker2"  ) . removeClass (  'element-invisible' ) ;                   \ $queue . parent ( ) . addClass ( 'filled' ) ;                   \ $queue . show ( ) ;                   \ $statusBar . removeClass ( 'element-invisible' ) ;                    uploader . refresh ( ) ;                    break ;                 case  'uploading' :                   \ $ (  "#$id_name .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  )  {                                              }  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 > php;         return $str;     }  

    三:接收上传文件的函数

    /Application/Common/Common/function.php

      /**  *    ajax 使  * @param  string   $path        /Upload/image/  * @param  string   $format    * @param  integer  $maxSize   52428800  * @return booler    ajax json  */ function ajax_upload($path='file',$format='empty',$maxSize='52428800'){     ini_set('max_execution_time', '0');     //  /     $path=trim($path,'/');     //  Upload     $path=strtolower(substr($path, 0,6))==='upload' ? ucfirst($path) : 'Upload/'.$path;     //      $ext_arr= array(              'image' => array('gif', 'jpg', 'jpeg', 'png', 'bmp'),              'photo' => array('jpg', 'jpeg', 'png'),              'flash' => array('swf', 'flv'),              'media' => array('swf', 'flv', 'mp3', 'wav', 'wma', 'wmv', 'mid', 'avi', 'mpg', 'asf', 'rm', 'rmvb'),              'file' => array('doc', 'docx', 'xls', 'xlsx', 'ppt', 'htm', 'html', 'txt', 'zip', 'rar', 'gz', 'bz2','pdf')         );     if(!empty($_FILES)){         //          $config=array(                   'maxSize'   =>  $maxSize,               //  50M                   'rootPath'  =>  './',                   //                    'savePath'  =>  './'.$path.'/',         //                    'saveName'  =>  array('uniqid',''),     //                    'autoSub'   =>  true,                   //  使   true                   'exts'      =>    isset($ext_arr[$format])?$ext_arr[$format]:'',              );         //          $upload=new \Think\Upload($config);         //          $info=$upload->upload();         $data=array();         if(!$info){              //               $error=$upload->getError();              $data['error_info']=$error;              echo json_encode($data);         }else{              //               foreach($info as $file){                   $data['name']=trim($file['savepath'].$file['savename'],'.');                   echo json_encode($data);              }                        }     } }  

    四:接收上传文件的控制器方法

    /Application/Home/Controller/IndexController.class.php

       /**      * webuploader       */     public function ajax_upload(){         //          ajax_upload('/Upload/image/');     }       /**      * webuploader  demo      */     public function webuploader(){         //  post           if(IS_POST){              p($_POST);die;         }else{              $this->display();         }     }  五:html中使用   <!DOCTYPE html> < html > < head >      < meta  charset = "UTF-8" >      < title >thinkphp webuploader </ title >      < webuploadercss  /> </ head > < body >   < form  action = "{:U('Home/Index/webuploader')}"  method = "post" >      < webuploader  name = "image"  url = "{:U('Home/Index/ajax_upload')}"  word = "300"  />      < input  type = "text"  name = "extend"  value = "" >      < input  type = "submit"  value = "" > </ form >     < webuploaderjs  /> </ body > </ html >

    前面这么墨迹麻烦的整合了半天;最主要就是为了这一步的使用;

    以后在需要上传的页面中;只需要添加三个标签就ok了;

    在head中添加<webuploadercss />;

    在body结束前添加 <webuploaderjs />;

    在需要需要上传的表单中添加<webuploader />;

    当然还可以自定义表单的名称、接收异步上传文件处理的url、提示文字;

    <webuploader name="image" url="{:U('Home/Index/ajax_upload')}" word="或将照片拖到这里,单次最多可选300张" />

    转自:白俊遥博客:http://baijunyao.com

    转载请注明原文地址: https://ju.6miu.com/read-32917.html

    最新回复(0)