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

来源:互联网 发布:商品期货持仓数据 编辑:程序博客网 时间:2024/05/29 17:52
webuplader是百度的一个前端开源上传插件,支持加密、分片上传。还是阔以的。
不过文档写的实在是不敢恭维,挫到爆,gettting start介绍快速开始,写的都是缺少东西的,直接复制下来是不可以运行的。
总结出一个经验,测试html最好还是使用jsp,不然修改了页面,浏览器上总是有缓存,清缓存是个很蛋疼的事情。
1. 引如外部资源 css,js文件
  这里${ctxStatic}不要管,这个只是spring项目中使用el表达式来写静态文件前缀了。使用的话,测试的话直接写死绝对路即可。

[html] view plain copy
  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

[html] view plain copy
  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.        // $list为容器jQuery实例  
  41.        $list.append( $li );  
  42.   
  43.        // 创建缩略图  
  44.        // 如果为非图片文件,可以不用调用此方法。  
  45.        // thumbnailWidth x thumbnailHeight 为 100 x 100  
  46.        uploader.makeThumb( file, function( error, src ) {   //webuploader方法  
  47.            if ( error ) {  
  48.                $img.replaceWith('<span>不能预览</span>');  
  49.                return;  
  50.            }  
  51.            $img.attr( 'src', src );  
  52.        }, thumbnailWidth, thumbnailHeight );  
  53.    });  
  54.    // 文件上传过程中创建进度条实时显示。  
  55.    uploader.on( 'uploadProgress'function( file, percentage ) {  
  56.        var $li = $( '#'+file.id ),  
  57.            $percent = $li.find('.progress span');  
  58.        // 避免重复创建  
  59.        if ( !$percent.length ) {  
  60.            $percent = $('<p class="progress"><span></span></p>')  
  61.                    .appendTo( $li )  
  62.                    .find('span');  
  63.        }  
  64.        $percent.css( 'width', percentage * 100 + '%' );  
  65.    });  
  66.    // 文件上传成功,给item添加成功class, 用样式标记上传成功。  
  67.    uploader.on( 'uploadSuccess'function( file ) {  
  68.        $( '#'+file.id ).addClass('upload-state-done');  
  69.    });  
  70.   
  71.    // 文件上传失败,显示上传出错。  
  72.    uploader.on( 'uploadError'function( file ) {  
  73.        var $li = $( '#'+file.id ),  
  74.            $error = $li.find('div.error');  
  75.        // 避免重复创建  
  76.        if ( !$error.length ) {  
  77.            $error = $('<div class="error"></div>').appendTo( $li );  
  78.        }  
  79.        $error.text('上传失败');  
  80.    });  
  81.    // 完成上传完了,成功或者失败,先删除进度条。  
  82.    uploader.on( 'uploadComplete'function( file ) {  
  83.        $( '#'+file.id ).find('.progress').remove();  
  84.    });  
  85.       $btn.on( 'click'function() {  
  86.         console.log("上传...");  
  87.         uploader.upload();  
  88.         console.log("上传成功");  
  89.       });  
  90.   });  
  91.  </script></span>  
0 0