分片、并发
分片与并发结合,将一个大文件分割成多块,并发上传,极大地提高大文件的上传速度。
当网络问题导致传输错误时,只需要重传出错分片,而不是整个文件。另外分片传输能够更加实时的跟踪上传进度。
预览、压缩
支持常用图片格式jpg,jpeg,gif,bmp,png预览与压缩,节省网络数据传输。
解析jpeg中的meta信息,对于各种orientation做了正确的处理,同时压缩后上传保留图片的所有原始meta数据。
多途径添加文件
支持文件多选,类型过滤,拖拽(文件&文件夹),图片粘贴功能。
粘贴功能主要体现在当有图片数据在剪切板中时(截屏工具如QQ(Ctrl + ALT + A), 网页中右击图片点击复制),Ctrl + V便可添加此图片文件。
HTML5 & FLASH
兼容主流浏览器,接口一致,实现了两套运行时支持,用户无需关心内部用了什么内核。
同时Flash部分没有做任何UI相关的工作,方便不关心flash的用户扩展和自定义业务需求。
MD5秒传
当文件体积大、量比较多时,支持上传前做文件md5值验证,一致则可直接跳过。
如果服务端与前端统一修改算法,取段md5,可大大提升验证性能,耗时在20ms左右。
易扩展、可拆分
采用可拆分机制, 将各个功能独立成了小组件,可自由搭配。
采用AMD规范组织代码,清晰明了,方便高级玩家扩展。
引入资源
使用Web Uploader文件上传需要引入三种资源:JS, CSS, SWF。
1 2 3 4 5 6 7 8 | < code class = "language-html" data-lang = "html" style = "box-sizing: border-box; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: inherit; padding: 0px; color: inherit; border-radius: 0px; background-color: transparent;" >< span class = "c" style = "box-sizing: border-box; color: rgb(153, 153, 136); font-style: italic;" > <!--引入CSS--> </ span > < span class = "nt" style = "box-sizing: border-box; color: rgb(0, 0, 128);" >< link </span> < span class = "na" style = "box-sizing: border-box; color: rgb(0, 128, 128);" >rel=</ span >< span class = "s" style = "box-sizing: border-box; color: rgb(221, 17, 68);" >"stylesheet"</ span > < span class = "na" style = "box-sizing: border-box; color: rgb(0, 128, 128);" >type=</ span >< span class = "s" style = "box-sizing: border-box; color: rgb(221, 17, 68);" >"text/css"</ span > < span class = "na" style = "box-sizing: border-box; color: rgb(0, 128, 128);" >href=</ span >< span class = "s" style = "box-sizing: border-box; color: rgb(221, 17, 68);" >"webuploader文件夹/webuploader.css"</ span >< span class = "nt" style = "box-sizing: border-box; color: rgb(0, 0, 128);" >></ span > < span class = "c" style = "box-sizing: border-box; color: rgb(153, 153, 136); font-style: italic;" > <!--引入JS--> </ span > < span class = "nt" style = "box-sizing: border-box; color: rgb(0, 0, 128);" >< script </span>< span class = "na" style = "box-sizing: border-box; color: rgb(0, 128, 128);" >type=</ span >< span class = "s" style = "box-sizing: border-box; color: rgb(221, 17, 68);" >"text/javascript"</ span > < span class = "na" style = "box-sizing: border-box; color: rgb(0, 128, 128);" >src=</ span >< span class = "s" style = "box-sizing: border-box; color: rgb(221, 17, 68);" >"webuploader文件夹/webuploader.js"</ span >< span class = "nt" style = "box-sizing: border-box; color: rgb(0, 0, 128);" >></ script ></ span > < span class = "c" style = "box-sizing: border-box; color: rgb(153, 153, 136); font-style: italic;" > <!--SWF在初始化的时候指定,在后面将展示--> </ span > </ code > |
WebUploader只包含文件上传的底层实现,不包括UI部分。所以交互方面可以自由发挥,以下将演示如何去实现一个简单的版本。
请点击下面的选择文件按钮,然后点击开始上传体验此Demo。
选择文件
开始上传
首先准备dom结构,包含存放文件信息的容器、选择按钮和上传按钮三个部分。
1 2 3 4 5 6 7 8 9 | < code class = "language-html" data-lang = "html" style = "box-sizing: border-box; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: inherit; padding: 0px; color: inherit; border-radius: 0px; background-color: transparent;" >< span class = "nt" style = "box-sizing: border-box; color: rgb(0, 0, 128);" >< div </span> < span class = "na" style = "box-sizing: border-box; color: rgb(0, 128, 128);" >id=</ span >< span class = "s" style = "box-sizing: border-box; color: rgb(221, 17, 68);" >"uploader"</ span > < span class = "na" style = "box-sizing: border-box; color: rgb(0, 128, 128);" >class=</ span >< span class = "s" style = "box-sizing: border-box; color: rgb(221, 17, 68);" >"wu-example"</ span >< span class = "nt" style = "box-sizing: border-box; color: rgb(0, 0, 128);" >></ span > < span class = "c" style = "box-sizing: border-box; color: rgb(153, 153, 136); font-style: italic;" > <!--用来存放文件信息--> </ span > < span class = "nt" style = "box-sizing: border-box; color: rgb(0, 0, 128);" >< div </span> < span class = "na" style = "box-sizing: border-box; color: rgb(0, 128, 128);" >id=</ span >< span class = "s" style = "box-sizing: border-box; color: rgb(221, 17, 68);" >"thelist"</ span > < span class = "na" style = "box-sizing: border-box; color: rgb(0, 128, 128);" >class=</ span >< span class = "s" style = "box-sizing: border-box; color: rgb(221, 17, 68);" >"uploader-list"</ span >< span class = "nt" style = "box-sizing: border-box; color: rgb(0, 0, 128);" >></ div ></ span > < span class = "nt" style = "box-sizing: border-box; color: rgb(0, 0, 128);" >< div </span> < span class = "na" style = "box-sizing: border-box; color: rgb(0, 128, 128);" >class=</ span >< span class = "s" style = "box-sizing: border-box; color: rgb(221, 17, 68);" >"btns"</ span >< span class = "nt" style = "box-sizing: border-box; color: rgb(0, 0, 128);" >></ span > < span class = "nt" style = "box-sizing: border-box; color: rgb(0, 0, 128);" >< div </span> < span class = "na" style = "box-sizing: border-box; color: rgb(0, 128, 128);" >id=</ span >< span class = "s" style = "box-sizing: border-box; color: rgb(221, 17, 68);" >"picker"</ span >< span class = "nt" style = "box-sizing: border-box; color: rgb(0, 0, 128);" >></ span >选择文件< span class = "nt" style = "box-sizing: border-box; color: rgb(0, 0, 128);" ></ div ></ span > < span class = "nt" style = "box-sizing: border-box; color: rgb(0, 0, 128);" >< button </span> < span class = "na" style = "box-sizing: border-box; color: rgb(0, 128, 128);" >id=</ span >< span class = "s" style = "box-sizing: border-box; color: rgb(221, 17, 68);" >"ctlBtn"</ span > < span class = "na" style = "box-sizing: border-box; color: rgb(0, 128, 128);" >class=</ span >< span class = "s" style = "box-sizing: border-box; color: rgb(221, 17, 68);" >"btn btn-default"</ span >< span class = "nt" style = "box-sizing: border-box; color: rgb(0, 0, 128);" >></ span >开始上传< span class = "nt" style = "box-sizing: border-box; color: rgb(0, 0, 128);" ></ button ></ span > < span class = "nt" style = "box-sizing: border-box; color: rgb(0, 0, 128);" ></ div ></ span > < span class = "nt" style = "box-sizing: border-box; color: rgb(0, 0, 128);" ></ div ></ span > </ code > |
具体说明,请看一下代码中的注释部分。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | <code class= "language-javascript" data-lang= "javascript" style= "box-sizing: border-box; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: inherit; padding: 0px; color: inherit; border-radius: 0px; background-color: transparent;" ><span class= "kd" style= "box-sizing: border-box; font-weight: bold;" > var </span> <span class= "nx" style= "box-sizing: border-box;" >uploader</span> <span class= "o" style= "box-sizing: border-box; font-weight: bold;" >=</span> <span class= "nx" style= "box-sizing: border-box;" >WebUploader</span><span class= "p" style= "box-sizing: border-box;" >.</span><span class= "nx" style= "box-sizing: border-box;" >create</span><span class= "p" style= "box-sizing: border-box;" >({</span> <span class= "c1" style= "box-sizing: border-box; color: rgb(153, 153, 136); font-style: italic;" > // swf文件路径</span> <span class= "nx" style= "box-sizing: border-box;" >swf</span><span class= "o" style= "box-sizing: border-box; font-weight: bold;" >:</span> <span class= "nx" style= "box-sizing: border-box;" >BASE_URL</span> <span class= "o" style= "box-sizing: border-box; font-weight: bold;" >+</span> <span class= "s1" style= "box-sizing: border-box; color: rgb(221, 17, 68);" > '/js/Uploader.swf' </span><span class= "p" style= "box-sizing: border-box;" >,</span> <span class= "c1" style= "box-sizing: border-box; color: rgb(153, 153, 136); font-style: italic;" > // 文件接收服务端。</span> <span class= "nx" style= "box-sizing: border-box;" >server</span><span class= "o" style= "box-sizing: border-box; font-weight: bold;" >:</span> <span class= "s1" style= "box-sizing: border-box; color: rgb(221, 17, 68);" > '/' </span><span class= "p" style= "box-sizing: border-box;" >,</span> <span class= "c1" style= "box-sizing: border-box; color: rgb(153, 153, 136); font-style: italic;" > // 选择文件的按钮。可选。</span> <span class= "c1" style= "box-sizing: border-box; color: rgb(153, 153, 136); font-style: italic;" > // 内部根据当前运行是创建,可能是input元素,也可能是flash.</span> <span class= "nx" style= "box-sizing: border-box;" >pick</span><span class= "o" style= "box-sizing: border-box; font-weight: bold;" >:</span> <span class= "s1" style= "box-sizing: border-box; color: rgb(221, 17, 68);" > '#picker' </span><span class= "p" style= "box-sizing: border-box;" >,</span> <span class= "c1" style= "box-sizing: border-box; color: rgb(153, 153, 136); font-style: italic;" > // 不压缩image, 默认如果是jpeg,文件上传前会压缩一把再上传!</span> <span class= "nx" style= "box-sizing: border-box;" >resize</span><span class= "o" style= "box-sizing: border-box; font-weight: bold;" >:</span> <span class= "kc" style= "box-sizing: border-box; font-weight: bold;" > false </span> <span class= "p" style= "box-sizing: border-box;" >});</span> </code> |
由于webuploader不处理UI逻辑,所以需要去监听fileQueued事件来实现。
1 2 3 4 5 6 7 8 | <code class= "language-javascript" data-lang= "javascript" style= "box-sizing: border-box; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: inherit; padding: 0px; color: inherit; border-radius: 0px; background-color: transparent;" ><span class= "c1" style= "box-sizing: border-box; color: rgb(153, 153, 136); font-style: italic;" > // 当有文件被添加进队列的时候</span> <span class= "nx" style= "box-sizing: border-box;" >uploader</span><span class= "p" style= "box-sizing: border-box;" >.</span><span class= "nx" style= "box-sizing: border-box;" >on</span><span class= "p" style= "box-sizing: border-box;" >(</span> <span class= "s1" style= "box-sizing: border-box; color: rgb(221, 17, 68);" > 'fileQueued' </span><span class= "p" style= "box-sizing: border-box;" >,</span> <span class= "kd" style= "box-sizing: border-box; font-weight: bold;" > function </span><span class= "p" style= "box-sizing: border-box;" >(</span> <span class= "nx" style= "box-sizing: border-box;" >file</span> <span class= "p" style= "box-sizing: border-box;" >)</span> <span class= "p" style= "box-sizing: border-box;" >{</span> <span class= "nx" style= "box-sizing: border-box;" >$list</span><span class= "p" style= "box-sizing: border-box;" >.</span><span class= "nx" style= "box-sizing: border-box;" >append</span><span class= "p" style= "box-sizing: border-box;" >(</span> <span class= "s1" style= "box-sizing: border-box; color: rgb(221, 17, 68);" > '<div id="' </span> <span class="o " style=" box-sizing: border-box; font-weight: bold; ">+</span> <span class=" nx " style=" box-sizing: border-box; ">file</span><span class=" p " style=" box-sizing: border-box; ">.</span><span class=" nx " style=" box-sizing: border-box; ">id</span> <span class=" o " style=" box-sizing: border-box; font-weight: bold; ">+</span> <span class=" s1 " style=" box-sizing: border-box; color: rgb(221, 17, 68); ">'" class= "item" >'</span> <span class= "o" style= "box-sizing: border-box; font-weight: bold;" >+</span> <span class= "s1" style= "box-sizing: border-box; color: rgb(221, 17, 68);" > '<h4 class="info">' </span> <span class= "o" style= "box-sizing: border-box; font-weight: bold;" >+</span> <span class= "nx" style= "box-sizing: border-box;" >file</span><span class= "p" style= "box-sizing: border-box;" >.</span><span class= "nx" style= "box-sizing: border-box;" >name</span> <span class= "o" style= "box-sizing: border-box; font-weight: bold;" >+</span> <span class= "s1" style= "box-sizing: border-box; color: rgb(221, 17, 68);" > '</h4>' </span> <span class= "o" style= "box-sizing: border-box; font-weight: bold;" >+</span> <span class= "s1" style= "box-sizing: border-box; color: rgb(221, 17, 68);" > '<p class="state">等待上传...</p>' </span> <span class= "o" style= "box-sizing: border-box; font-weight: bold;" >+</span> <span class= "s1" style= "box-sizing: border-box; color: rgb(221, 17, 68);" > '</div>' </span> <span class= "p" style= "box-sizing: border-box;" >);</span> <span class= "p" style= "box-sizing: border-box;" >});</span> </code> |
文件上传中,Web Uploader会对外派送uploadProgress事件,其中包含文件对象和该文件当前上传进度。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | <code class= "language-javascript" data-lang= "javascript" style= "box-sizing: border-box; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: inherit; padding: 0px; color: inherit; border-radius: 0px; background-color: transparent;" ><span class= "c1" style= "box-sizing: border-box; color: rgb(153, 153, 136); font-style: italic;" > // 文件上传过程中创建进度条实时显示。</span> <span class= "nx" style= "box-sizing: border-box;" >uploader</span><span class= "p" style= "box-sizing: border-box;" >.</span><span class= "nx" style= "box-sizing: border-box;" >on</span><span class= "p" style= "box-sizing: border-box;" >(</span> <span class= "s1" style= "box-sizing: border-box; color: rgb(221, 17, 68);" > 'uploadProgress' </span><span class= "p" style= "box-sizing: border-box;" >,</span> <span class= "kd" style= "box-sizing: border-box; font-weight: bold;" > function </span><span class= "p" style= "box-sizing: border-box;" >(</span> <span class= "nx" style= "box-sizing: border-box;" >file</span><span class= "p" style= "box-sizing: border-box;" >,</span> <span class= "nx" style= "box-sizing: border-box;" >percentage</span> <span class= "p" style= "box-sizing: border-box;" >)</span> <span class= "p" style= "box-sizing: border-box;" >{</span> <span class= "kd" style= "box-sizing: border-box; font-weight: bold;" > var </span> <span class= "nx" style= "box-sizing: border-box;" >$li</span> <span class= "o" style= "box-sizing: border-box; font-weight: bold;" >=</span> <span class= "nx" style= "box-sizing: border-box;" >$</span><span class= "p" style= "box-sizing: border-box;" >(</span> <span class= "s1" style= "box-sizing: border-box; color: rgb(221, 17, 68);" > '#' </span><span class= "o" style= "box-sizing: border-box; font-weight: bold;" >+</span><span class= "nx" style= "box-sizing: border-box;" >file</span><span class= "p" style= "box-sizing: border-box;" >.</span><span class= "nx" style= "box-sizing: border-box;" >id</span> <span class= "p" style= "box-sizing: border-box;" >),</span> <span class= "nx" style= "box-sizing: border-box;" >$percent</span> <span class= "o" style= "box-sizing: border-box; font-weight: bold;" >=</span> <span class= "nx" style= "box-sizing: border-box;" >$li</span><span class= "p" style= "box-sizing: border-box;" >.</span><span class= "nx" style= "box-sizing: border-box;" >find</span><span class= "p" style= "box-sizing: border-box;" >(</span><span class= "s1" style= "box-sizing: border-box; color: rgb(221, 17, 68);" > '.progress .progress-bar' </span><span class= "p" style= "box-sizing: border-box;" >);</span> <span class= "c1" style= "box-sizing: border-box; color: rgb(153, 153, 136); font-style: italic;" > // 避免重复创建</span> <span class= "k" style= "box-sizing: border-box; font-weight: bold;" > if </span> <span class= "p" style= "box-sizing: border-box;" >(</span> <span class= "o" style= "box-sizing: border-box; font-weight: bold;" >!</span><span class= "nx" style= "box-sizing: border-box;" >$percent</span><span class= "p" style= "box-sizing: border-box;" >.</span><span class= "nx" style= "box-sizing: border-box;" >length</span> <span class= "p" style= "box-sizing: border-box;" >)</span> <span class= "p" style= "box-sizing: border-box;" >{</span> <span class= "nx" style= "box-sizing: border-box;" >$percent</span> <span class= "o" style= "box-sizing: border-box; font-weight: bold;" >=</span> <span class= "nx" style= "box-sizing: border-box;" >$</span><span class= "p" style= 特别申明:
|