Webuploader教程(一)------简单实用上传功能

webuplader是百度的一个前端开源上传插件,支持加密、分片上传。还是阔以的。
不过文档写的实在是不敢恭维,挫到爆,gettting start介绍快速开始,写的都是缺少东西的,直接复制下来是不可以运行的。
总结出一个经验,测试html最好还是使用jsp,不然修改了页面,浏览器上总是有缓存,清缓存是个很蛋疼的事情。
  这里${ctxStatic}不要管,这个只是spring项目中使用el表达式来写静态文件前缀了。使用的话,测试的话直接写死绝对路即可。

  1. <span style="font-size:14px;"<link href="${ctxStatic }/bootstrap-3.3.5-dist/css/bootstrap.min.css" type="text/css" rel="stylesheet"/>  
  2.  <link href="${ctxStatic }/webupload/css/webuploader.css" type="text/css" rel="stylesheet"/>  
  3.  <script type="text/javascript" src="${ctxStatic }/jquery/jquery-1.9.1.min.js"></script>  
  4.  <script type="text/javascript" src="${ctxStatic }/webupload/webuploader.min.js"></script>  
  5.  <script type="text/javascript" src="${ctxStatic }/bootstrap-3.3.5-dist/js/bootstrap.min.js"></script></span>  


2.写容器DOM
用来放置webuploader的dom

  1. <span style="font-size:14px;"><div id="uploader-demo">  
  2.     <!--用来存放item-->  
  3.     <div id="thelist" class="uploader-list"></div>  
  4.     <div>  
  5.      <div id="filePicker">选择图片</div>  
  6.      <button id="ctlBtn" class="btn btn-default">开始上传</button>  
  7.     </div>  
  8. </div>  
  9. </span>  

3. 初始化webuploader组件,设置上传等事件监听。

[javascript] view plain copy
    1. <span style="font-size:14px;"><script type="text/javascript">  
    2.   $(function(){  
    3.    /*init webuploader*/  
    4.    var $list=$("#thelist");   //这几个初始化全局的百度文档上没说明,好蛋疼。  
    5.    var $btn =$("#ctlBtn");   //开始上传  
    6.    var thumbnailWidth = 100;   //缩略图高度和宽度 (单位是像素),当宽高度是0~1的时候,是按照百分比计算,具体可以看api文档  
    7.    var thumbnailHeight = 100;  
    8.   
    9.    var uploader = WebUploader.create({  
    10.        // 选完文件后,是否自动上传。  
    11.        auto: false,  
    12.   
    13.        // swf文件路径  
    14.        swf: '${ctxStatic }/webupload/Uploader.swf',  
    15.   
    16.        // 文件接收服务端。  
    17.        server: '/apm-web/a/test/',  
    18.   
    19.        // 选择文件的按钮。可选。  
    20.        // 内部根据当前运行是创建,可能是input元素,也可能是flash.  
    21.        pick: '#filePicker',  
    22.   
    23.        // 只允许选择图片文件。  
    24.        accept: {  
    25.            title: 'Images',  
    26.            extensions: 'gif,jpg,jpeg,bmp,png',  
    27.            mimeTypes: 'image/*'  
    28.        },  
    29.        method:'POST',  
    30.    });  
    31.    // 当有文件添加进来的时候  
    32.    uploader.on( 'fileQueued', function( file ) {  // webuploader事件.当选择文件后,文件被加载到文件队列中,触发该事件。等效于 uploader.onFileueued = function(file){...} ,类似js的事件定义。  
    33.        var $li = $(  
    34.                '<div id="' + file.id + '" class="file-item thumbnail">' +  
    35.                    '<img>' +  
    36.                    '<div class="info">' + file.name + '</div>' +  
    37.                '</div>'  
    38.                ),  
    39.            $img = $li.find('img');  
    40.   
    41.   
    42.        // $list为容器jQuery实例  
    43.        $list.append( $li );  
    44.   
    45.        // 创建缩略图  
    46.        // 如果为非图片文件,可以不用调用此方法。  
    47.        // thumbnailWidth x thumbnailHeight 为 100 x 100  
    48.        uploader.makeThumb( file, function( error, src ) {   //webuploader方法  
    49.            if ( error ) {  
    50.                $img.replaceWith('<span>不能预览</span>');  
    51.                return;  
    52.            }  
    53.   
    54.            $img.attr( 'src', src );  
    55.        }, thumbnailWidth, thumbnailHeight );  
    56.    });  
    57.    // 文件上传过程中创建进度条实时显示。  
    58.    uploader.on( 'uploadProgress', function( file, percentage ) {  
    59.        var $li = $( '#'+file.id ),  
    60.            $percent = $li.find('.progress span');  
    61.   
    62.        // 避免重复创建  
    63.        if ( !$percent.length ) {  
    64.            $percent = $('<p class="progress"><span></span></p>')  
    65.                    .appendTo( $li )  
    66.                    .find('span');  
    67.        }  
    68.   
    69.        $percent.css( 'width', percentage * 100 + '%' );  
    70.    });  
    71.   
    72.    // 文件上传成功,给item添加成功class, 用样式标记上传成功。  
    73.    uploader.on( 'uploadSuccess', function( file ) {  
    74.        $( '#'+file.id ).addClass('upload-state-done');  
    75.    });  
    76.   
    77.    // 文件上传失败,显示上传出错。  
    78.    uploader.on( 'uploadError', function( file ) {  
    79.        var $li = $( '#'+file.id ),  
    80.            $error = $li.find('div.error');  
    81.   
    82.        // 避免重复创建  
    83.        if ( !$error.length ) {  
    84.            $error = $('<div class="error"></div>').appendTo( $li );  
    85.        }  
    86.   
    87.        $error.text('上传失败');  
    88.    });  
    89.   
    90.    // 完成上传完了,成功或者失败,先删除进度条。  
    91.    uploader.on( 'uploadComplete', function( file ) {  
    92.        $( '#'+file.id ).find('.progress').remove();  
    93.    });  
    94.       $btn.on( 'click', function() {  
    95.         console.log("上传...");  
    96.         uploader.upload();  
    97.         console.log("上传成功");  
    98.       });  
    99.   });  
    100.  </script></span> 
posted @ 2017-11-20 17:05  jamescr7  阅读(449)  评论(0编辑  收藏  举报