webuploader-demo.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. jQuery(function() {
  2. var $ = jQuery, // just in case. Make sure it's not an other libaray.
  3. $wrap = $('#uploader'),
  4. // 图片容器
  5. $queue = $('<ul class="filelist"></ul>')
  6. .appendTo( $wrap.find('.queueList') ),
  7. // 状态栏,包括进度和控制按钮
  8. $statusBar = $wrap.find('.statusBar'),
  9. // 文件总体选择信息。
  10. $info = $statusBar.find('.info'),
  11. // 上传按钮
  12. $upload = $wrap.find('.uploadBtn'),
  13. // 没选择文件之前的内容。
  14. $placeHolder = $wrap.find('.placeholder'),
  15. // 总体进度条
  16. $progress = $statusBar.find('.progress').hide(),
  17. // 添加的文件数量
  18. fileCount = 0,
  19. // 添加的文件总大小
  20. fileSize = 0,
  21. // 优化retina, 在retina下这个值是2
  22. ratio = window.devicePixelRatio || 1,
  23. // 缩略图大小
  24. thumbnailWidth = 110 * ratio,
  25. thumbnailHeight = 110 * ratio,
  26. // 可能有pedding, ready, uploading, confirm, done.
  27. state = 'pedding',
  28. // 所有文件的进度信息,key为file id
  29. percentages = {},
  30. supportTransition = (function(){
  31. var s = document.createElement('p').style,
  32. r = 'transition' in s ||
  33. 'WebkitTransition' in s ||
  34. 'MozTransition' in s ||
  35. 'msTransition' in s ||
  36. 'OTransition' in s;
  37. s = null;
  38. return r;
  39. })(),
  40. // WebUploader实例
  41. uploader;
  42. if ( !WebUploader.Uploader.support() ) {
  43. alert( 'Web Uploader 不支持您的浏览器!如果你使用的是IE浏览器,请尝试升级 flash 播放器');
  44. throw new Error( 'WebUploader does not support the browser you are using.' );
  45. }
  46. // 实例化
  47. uploader = WebUploader.create({
  48. pick: {
  49. id: '#filePicker',
  50. label: '点击选择图片'
  51. },
  52. dnd: '#uploader .queueList',
  53. paste: document.body,
  54. accept: {
  55. title: 'Images',
  56. extensions: 'gif,jpg,jpeg,bmp,png',
  57. mimeTypes: 'image/*'
  58. },
  59. // swf文件路径
  60. swf: BASE_URL + '/Uploader.swf',
  61. disableGlobalDnd: true,
  62. chunked: true,
  63. // server: 'http://webuploader.duapp.com/server/fileupload.php',
  64. server: 'http://2betop.net/fileupload.php',
  65. fileNumLimit: 300,
  66. fileSizeLimit: 5 * 1024 * 1024, // 200 M
  67. fileSingleSizeLimit: 1 * 1024 * 1024 // 50 M
  68. });
  69. // 添加“添加文件”的按钮,
  70. uploader.addButton({
  71. id: '#filePicker2',
  72. label: '继续添加'
  73. });
  74. // 当有文件添加进来时执行,负责view的创建
  75. function addFile( file ) {
  76. var $li = $( '<li id="' + file.id + '">' +
  77. '<p class="title">' + file.name + '</p>' +
  78. '<p class="imgWrap"></p>'+
  79. '<p class="progress"><span></span></p>' +
  80. '</li>' ),
  81. $btns = $('<div class="file-panel">' +
  82. '<span class="cancel">删除</span>' +
  83. '<span class="rotateRight">向右旋转</span>' +
  84. '<span class="rotateLeft">向左旋转</span></div>').appendTo( $li ),
  85. $prgress = $li.find('p.progress span'),
  86. $wrap = $li.find( 'p.imgWrap' ),
  87. $info = $('<p class="error"></p>'),
  88. showError = function( code ) {
  89. switch( code ) {
  90. case 'exceed_size':
  91. text = '文件大小超出';
  92. break;
  93. case 'interrupt':
  94. text = '上传暂停';
  95. break;
  96. default:
  97. text = '上传失败,请重试';
  98. break;
  99. }
  100. $info.text( text ).appendTo( $li );
  101. };
  102. if ( file.getStatus() === 'invalid' ) {
  103. showError( file.statusText );
  104. } else {
  105. // @todo lazyload
  106. $wrap.text( '预览中' );
  107. uploader.makeThumb( file, function( error, src ) {
  108. if ( error ) {
  109. $wrap.text( '不能预览' );
  110. return;
  111. }
  112. var img = $('<img src="'+src+'">');
  113. $wrap.empty().append( img );
  114. }, thumbnailWidth, thumbnailHeight );
  115. percentages[ file.id ] = [ file.size, 0 ];
  116. file.rotation = 0;
  117. }
  118. file.on('statuschange', function( cur, prev ) {
  119. if ( prev === 'progress' ) {
  120. $prgress.hide().width(0);
  121. } else if ( prev === 'queued' ) {
  122. $li.off( 'mouseenter mouseleave' );
  123. $btns.remove();
  124. }
  125. // 成功
  126. if ( cur === 'error' || cur === 'invalid' ) {
  127. console.log( file.statusText );
  128. showError( file.statusText );
  129. percentages[ file.id ][ 1 ] = 1;
  130. } else if ( cur === 'interrupt' ) {
  131. showError( 'interrupt' );
  132. } else if ( cur === 'queued' ) {
  133. percentages[ file.id ][ 1 ] = 0;
  134. } else if ( cur === 'progress' ) {
  135. $info.remove();
  136. $prgress.css('display', 'block');
  137. } else if ( cur === 'complete' ) {
  138. $li.append( '<span class="success"></span>' );
  139. }
  140. $li.removeClass( 'state-' + prev ).addClass( 'state-' + cur );
  141. });
  142. $li.on( 'mouseenter', function() {
  143. $btns.stop().animate({height: 30});
  144. });
  145. $li.on( 'mouseleave', function() {
  146. $btns.stop().animate({height: 0});
  147. });
  148. $btns.on( 'click', 'span', function() {
  149. var index = $(this).index(),
  150. deg;
  151. switch ( index ) {
  152. case 0:
  153. uploader.removeFile( file );
  154. return;
  155. case 1:
  156. file.rotation += 90;
  157. break;
  158. case 2:
  159. file.rotation -= 90;
  160. break;
  161. }
  162. if ( supportTransition ) {
  163. deg = 'rotate(' + file.rotation + 'deg)';
  164. $wrap.css({
  165. '-webkit-transform': deg,
  166. '-mos-transform': deg,
  167. '-o-transform': deg,
  168. 'transform': deg
  169. });
  170. } else {
  171. $wrap.css( 'filter', 'progid:DXImageTransform.Microsoft.BasicImage(rotation='+ (~~((file.rotation/90)%4 + 4)%4) +')');
  172. // use jquery animate to rotation
  173. // $({
  174. // rotation: rotation
  175. // }).animate({
  176. // rotation: file.rotation
  177. // }, {
  178. // easing: 'linear',
  179. // step: function( now ) {
  180. // now = now * Math.PI / 180;
  181. // var cos = Math.cos( now ),
  182. // sin = Math.sin( now );
  183. // $wrap.css( 'filter', "progid:DXImageTransform.Microsoft.Matrix(M11=" + cos + ",M12=" + (-sin) + ",M21=" + sin + ",M22=" + cos + ",SizingMethod='auto expand')");
  184. // }
  185. // });
  186. }
  187. });
  188. $li.appendTo( $queue );
  189. }
  190. // 负责view的销毁
  191. function removeFile( file ) {
  192. var $li = $('#'+file.id);
  193. delete percentages[ file.id ];
  194. updateTotalProgress();
  195. $li.off().find('.file-panel').off().end().remove();
  196. }
  197. function updateTotalProgress() {
  198. var loaded = 0,
  199. total = 0,
  200. spans = $progress.children(),
  201. percent;
  202. $.each( percentages, function( k, v ) {
  203. total += v[ 0 ];
  204. loaded += v[ 0 ] * v[ 1 ];
  205. } );
  206. percent = total ? loaded / total : 0;
  207. spans.eq( 0 ).text( Math.round( percent * 100 ) + '%' );
  208. spans.eq( 1 ).css( 'width', Math.round( percent * 100 ) + '%' );
  209. updateStatus();
  210. }
  211. function updateStatus() {
  212. var text = '', stats;
  213. if ( state === 'ready' ) {
  214. text = '选中' + fileCount + '张图片,共' +
  215. WebUploader.formatSize( fileSize ) + '。';
  216. } else if ( state === 'confirm' ) {
  217. stats = uploader.getStats();
  218. if ( stats.uploadFailNum ) {
  219. text = '已成功上传' + stats.successNum+ '张照片至XX相册,'+
  220. stats.uploadFailNum + '张照片上传失败,<a class="retry" href="#">重新上传</a>失败图片或<a class="ignore" href="#">忽略</a>'
  221. }
  222. } else {
  223. stats = uploader.getStats();
  224. text = '共' + fileCount + '张(' +
  225. WebUploader.formatSize( fileSize ) +
  226. '),已上传' + stats.successNum + '张';
  227. if ( stats.uploadFailNum ) {
  228. text += ',失败' + stats.uploadFailNum + '张';
  229. }
  230. }
  231. $info.html( text );
  232. }
  233. function setState( val ) {
  234. var file, stats;
  235. if ( val === state ) {
  236. return;
  237. }
  238. $upload.removeClass( 'state-' + state );
  239. $upload.addClass( 'state-' + val );
  240. state = val;
  241. switch ( state ) {
  242. case 'pedding':
  243. $placeHolder.removeClass( 'element-invisible' );
  244. $queue.parent().removeClass('filled');
  245. $queue.hide();
  246. $statusBar.addClass( 'element-invisible' );
  247. uploader.refresh();
  248. break;
  249. case 'ready':
  250. $placeHolder.addClass( 'element-invisible' );
  251. $( '#filePicker2' ).removeClass( 'element-invisible');
  252. $queue.parent().addClass('filled');
  253. $queue.show();
  254. $statusBar.removeClass('element-invisible');
  255. uploader.refresh();
  256. break;
  257. case 'uploading':
  258. $( '#filePicker2' ).addClass( 'element-invisible' );
  259. $progress.show();
  260. $upload.text( '暂停上传' );
  261. break;
  262. case 'paused':
  263. $progress.show();
  264. $upload.text( '继续上传' );
  265. break;
  266. case 'confirm':
  267. $progress.hide();
  268. $upload.text( '开始上传' ).addClass( 'disabled' );
  269. stats = uploader.getStats();
  270. if ( stats.successNum && !stats.uploadFailNum ) {
  271. setState( 'finish' );
  272. return;
  273. }
  274. break;
  275. case 'finish':
  276. stats = uploader.getStats();
  277. if ( stats.successNum ) {
  278. alert( '上传成功' );
  279. } else {
  280. // 没有成功的图片,重设
  281. state = 'done';
  282. location.reload();
  283. }
  284. break;
  285. }
  286. updateStatus();
  287. }
  288. uploader.onUploadProgress = function( file, percentage ) {
  289. var $li = $('#'+file.id),
  290. $percent = $li.find('.progress span');
  291. $percent.css( 'width', percentage * 100 + '%' );
  292. percentages[ file.id ][ 1 ] = percentage;
  293. updateTotalProgress();
  294. };
  295. uploader.onFileQueued = function( file ) {
  296. fileCount++;
  297. fileSize += file.size;
  298. if ( fileCount === 1 ) {
  299. $placeHolder.addClass( 'element-invisible' );
  300. $statusBar.show();
  301. }
  302. addFile( file );
  303. setState( 'ready' );
  304. updateTotalProgress();
  305. };
  306. uploader.onFileDequeued = function( file ) {
  307. fileCount--;
  308. fileSize -= file.size;
  309. if ( !fileCount ) {
  310. setState( 'pedding' );
  311. }
  312. removeFile( file );
  313. updateTotalProgress();
  314. };
  315. uploader.on( 'all', function( type ) {
  316. var stats;
  317. switch( type ) {
  318. case 'uploadFinished':
  319. setState( 'confirm' );
  320. break;
  321. case 'startUpload':
  322. setState( 'uploading' );
  323. break;
  324. case 'stopUpload':
  325. setState( 'paused' );
  326. break;
  327. }
  328. });
  329. uploader.onError = function( code ) {
  330. alert( 'Eroor: ' + code );
  331. };
  332. $upload.on('click', function() {
  333. if ( $(this).hasClass( 'disabled' ) ) {
  334. return false;
  335. }
  336. if ( state === 'ready' ) {
  337. uploader.upload();
  338. } else if ( state === 'paused' ) {
  339. uploader.upload();
  340. } else if ( state === 'uploading' ) {
  341. uploader.stop();
  342. }
  343. });
  344. $info.on( 'click', '.retry', function() {
  345. uploader.retry();
  346. } );
  347. $info.on( 'click', '.ignore', function() {
  348. alert( 'todo' );
  349. } );
  350. $upload.addClass( 'state-' + state );
  351. updateTotalProgress();
  352. });