upload.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  1. (function( $ ){
  2. // 当domReady的时候开始初始化
  3. $(function() {
  4. var $wrap = $('.uploader-list-container'),
  5. // 图片容器
  6. $queue = $( '<ul class="filelist"></ul>' )
  7. .appendTo( $wrap.find( '.queueList' ) ),
  8. // 状态栏,包括进度和控制按钮
  9. $statusBar = $wrap.find( '.statusBar' ),
  10. // 文件总体选择信息。
  11. $info = $statusBar.find( '.info' ),
  12. // 上传按钮
  13. $upload = $wrap.find( '.uploadBtn' ),
  14. // 没选择文件之前的内容。
  15. $placeHolder = $wrap.find( '.placeholder' ),
  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. // 判断浏览器是否支持图片的base64
  31. isSupportBase64 = ( function() {
  32. var data = new Image();
  33. var support = true;
  34. data.onload = data.onerror = function() {
  35. if( this.width != 1 || this.height != 1 ) {
  36. support = false;
  37. }
  38. }
  39. data.src = "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///ywAAAAAAQABAAACAUwAOw==";
  40. return support;
  41. } )(),
  42. // 检测是否已经安装flash,检测flash的版本
  43. flashVersion = ( function() {
  44. var version;
  45. try {
  46. version = navigator.plugins[ 'Shockwave Flash' ];
  47. version = version.description;
  48. } catch ( ex ) {
  49. try {
  50. version = new ActiveXObject('ShockwaveFlash.ShockwaveFlash')
  51. .GetVariable('$version');
  52. } catch ( ex2 ) {
  53. version = '0.0';
  54. }
  55. }
  56. version = version.match( /\d+/g );
  57. return parseFloat( version[ 0 ] + '.' + version[ 1 ], 10 );
  58. } )(),
  59. supportTransition = (function(){
  60. var s = document.createElement('p').style,
  61. r = 'transition' in s ||
  62. 'WebkitTransition' in s ||
  63. 'MozTransition' in s ||
  64. 'msTransition' in s ||
  65. 'OTransition' in s;
  66. s = null;
  67. return r;
  68. })(),
  69. // WebUploader实例
  70. uploader;
  71. if ( !WebUploader.Uploader.support('flash') && WebUploader.browser.ie ) {
  72. // flash 安装了但是版本过低。
  73. if (flashVersion) {
  74. (function(container) {
  75. window['expressinstallcallback'] = function( state ) {
  76. switch(state) {
  77. case 'Download.Cancelled':
  78. alert('您取消了更新!')
  79. break;
  80. case 'Download.Failed':
  81. alert('安装失败')
  82. break;
  83. default:
  84. alert('安装已成功,请刷新!');
  85. break;
  86. }
  87. delete window['expressinstallcallback'];
  88. };
  89. var swf = 'expressInstall.swf';
  90. // insert flash object
  91. var html = '<object type="application/' +
  92. 'x-shockwave-flash" data="' + swf + '" ';
  93. if (WebUploader.browser.ie) {
  94. html += 'classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" ';
  95. }
  96. html += 'width="100%" height="100%" style="outline:0">' +
  97. '<param name="movie" value="' + swf + '" />' +
  98. '<param name="wmode" value="transparent" />' +
  99. '<param name="allowscriptaccess" value="always" />' +
  100. '</object>';
  101. container.html(html);
  102. })($wrap);
  103. // 压根就没有安转。
  104. } else {
  105. $wrap.html('<a href="http://www.adobe.com/go/getflashplayer" target="_blank" border="0"><img alt="get flash player" src="http://www.adobe.com/macromedia/style_guide/images/160x41_Get_Flash_Player.jpg" /></a>');
  106. }
  107. return;
  108. } else if (!WebUploader.Uploader.support()) {
  109. alert( 'Web Uploader 不支持您的浏览器!');
  110. return;
  111. }
  112. // 实例化
  113. uploader = WebUploader.create({
  114. pick: {
  115. id: '#filePicker-2',
  116. label: '点击选择图片'
  117. },
  118. formData: {
  119. uid: 123
  120. },
  121. dnd: '#dndArea',
  122. paste: '#uploader',
  123. swf: '../Uploader.swf',
  124. chunked: false,
  125. chunkSize: 512 * 1024,
  126. server: '../server/fileupload.php',
  127. // runtimeOrder: 'flash',
  128. // accept: {
  129. // title: 'Images',
  130. // extensions: 'gif,jpg,jpeg,bmp,png',
  131. // mimeTypes: 'image/*'
  132. // },
  133. // 禁掉全局的拖拽功能。这样不会出现图片拖进页面的时候,把图片打开。
  134. disableGlobalDnd: true,
  135. fileNumLimit: 300,
  136. fileSizeLimit: 200 * 1024 * 1024, // 200 M
  137. fileSingleSizeLimit: 50 * 1024 * 1024 // 50 M
  138. });
  139. // 拖拽时不接受 js, txt 文件。
  140. uploader.on( 'dndAccept', function( items ) {
  141. var denied = false,
  142. len = items.length,
  143. i = 0,
  144. // 修改js类型
  145. unAllowed = 'text/plain;application/javascript ';
  146. for ( ; i < len; i++ ) {
  147. // 如果在列表里面
  148. if ( ~unAllowed.indexOf( items[ i ].type ) ) {
  149. denied = true;
  150. break;
  151. }
  152. }
  153. return !denied;
  154. });
  155. uploader.on('dialogOpen', function() {
  156. console.log('here');
  157. });
  158. // uploader.on('filesQueued', function() {
  159. // uploader.sort(function( a, b ) {
  160. // if ( a.name < b.name )
  161. // return -1;
  162. // if ( a.name > b.name )
  163. // return 1;
  164. // return 0;
  165. // });
  166. // });
  167. // 添加“添加文件”的按钮,
  168. uploader.addButton({
  169. id: '#filePicker2',
  170. label: '继续添加'
  171. });
  172. uploader.on('ready', function() {
  173. window.uploader = uploader;
  174. });
  175. // 当有文件添加进来时执行,负责view的创建
  176. function addFile( file ) {
  177. var $li = $( '<li id="' + file.id + '">' +
  178. '<p class="title">' + file.name + '</p>' +
  179. '<p class="imgWrap"></p>'+
  180. '<p class="progress"><span></span></p>' +
  181. '</li>' ),
  182. $btns = $('<div class="file-panel">' +
  183. '<span class="cancel">删除</span>' +
  184. '<span class="rotateRight">向右旋转</span>' +
  185. '<span class="rotateLeft">向左旋转</span></div>').appendTo( $li ),
  186. $prgress = $li.find('p.progress span'),
  187. $wrap = $li.find( 'p.imgWrap' ),
  188. $info = $('<p class="error"></p>'),
  189. showError = function( code ) {
  190. switch( code ) {
  191. case 'exceed_size':
  192. text = '文件大小超出';
  193. break;
  194. case 'interrupt':
  195. text = '上传暂停';
  196. break;
  197. default:
  198. text = '上传失败,请重试';
  199. break;
  200. }
  201. $info.text( text ).appendTo( $li );
  202. };
  203. if ( file.getStatus() === 'invalid' ) {
  204. showError( file.statusText );
  205. } else {
  206. // @todo lazyload
  207. $wrap.text( '预览中' );
  208. uploader.makeThumb( file, function( error, src ) {
  209. var img;
  210. if ( error ) {
  211. $wrap.text( '不能预览' );
  212. return;
  213. }
  214. if( isSupportBase64 ) {
  215. img = $('<img src="'+src+'">');
  216. $wrap.empty().append( img );
  217. } else {
  218. $.ajax('../server/preview.php', {
  219. method: 'POST',
  220. data: src,
  221. dataType:'json'
  222. }).done(function( response ) {
  223. if (response.result) {
  224. img = $('<img src="'+response.result+'">');
  225. $wrap.empty().append( img );
  226. } else {
  227. $wrap.text("预览出错");
  228. }
  229. });
  230. }
  231. }, thumbnailWidth, thumbnailHeight );
  232. percentages[ file.id ] = [ file.size, 0 ];
  233. file.rotation = 0;
  234. }
  235. file.on('statuschange', function( cur, prev ) {
  236. if ( prev === 'progress' ) {
  237. $prgress.hide().width(0);
  238. } else if ( prev === 'queued' ) {
  239. $li.off( 'mouseenter mouseleave' );
  240. $btns.remove();
  241. }
  242. // 成功
  243. if ( cur === 'error' || cur === 'invalid' ) {
  244. console.log( file.statusText );
  245. showError( file.statusText );
  246. percentages[ file.id ][ 1 ] = 1;
  247. } else if ( cur === 'interrupt' ) {
  248. showError( 'interrupt' );
  249. } else if ( cur === 'queued' ) {
  250. percentages[ file.id ][ 1 ] = 0;
  251. } else if ( cur === 'progress' ) {
  252. $info.remove();
  253. $prgress.css('display', 'block');
  254. } else if ( cur === 'complete' ) {
  255. $li.append( '<span class="success"></span>' );
  256. }
  257. $li.removeClass( 'state-' + prev ).addClass( 'state-' + cur );
  258. });
  259. $li.on( 'mouseenter', function() {
  260. $btns.stop().animate({height: 30});
  261. });
  262. $li.on( 'mouseleave', function() {
  263. $btns.stop().animate({height: 0});
  264. });
  265. $btns.on( 'click', 'span', function() {
  266. var index = $(this).index(),
  267. deg;
  268. switch ( index ) {
  269. case 0:
  270. uploader.removeFile( file );
  271. return;
  272. case 1:
  273. file.rotation += 90;
  274. break;
  275. case 2:
  276. file.rotation -= 90;
  277. break;
  278. }
  279. if ( supportTransition ) {
  280. deg = 'rotate(' + file.rotation + 'deg)';
  281. $wrap.css({
  282. '-webkit-transform': deg,
  283. '-mos-transform': deg,
  284. '-o-transform': deg,
  285. 'transform': deg
  286. });
  287. } else {
  288. $wrap.css( 'filter', 'progid:DXImageTransform.Microsoft.BasicImage(rotation='+ (~~((file.rotation/90)%4 + 4)%4) +')');
  289. // use jquery animate to rotation
  290. // $({
  291. // rotation: rotation
  292. // }).animate({
  293. // rotation: file.rotation
  294. // }, {
  295. // easing: 'linear',
  296. // step: function( now ) {
  297. // now = now * Math.PI / 180;
  298. // var cos = Math.cos( now ),
  299. // sin = Math.sin( now );
  300. // $wrap.css( 'filter', "progid:DXImageTransform.Microsoft.Matrix(M11=" + cos + ",M12=" + (-sin) + ",M21=" + sin + ",M22=" + cos + ",SizingMethod='auto expand')");
  301. // }
  302. // });
  303. }
  304. });
  305. $li.appendTo( $queue );
  306. }
  307. // 负责view的销毁
  308. function removeFile( file ) {
  309. var $li = $('#'+file.id);
  310. delete percentages[ file.id ];
  311. updateTotalProgress();
  312. $li.off().find('.file-panel').off().end().remove();
  313. }
  314. function updateTotalProgress() {
  315. var loaded = 0,
  316. total = 0,
  317. spans = $progress.children(),
  318. percent;
  319. $.each( percentages, function( k, v ) {
  320. total += v[ 0 ];
  321. loaded += v[ 0 ] * v[ 1 ];
  322. } );
  323. percent = total ? loaded / total : 0;
  324. spans.eq( 0 ).text( Math.round( percent * 100 ) + '%' );
  325. spans.eq( 1 ).css( 'width', Math.round( percent * 100 ) + '%' );
  326. updateStatus();
  327. }
  328. function updateStatus() {
  329. var text = '', stats;
  330. if ( state === 'ready' ) {
  331. text = '选中' + fileCount + '张图片,共' +
  332. WebUploader.formatSize( fileSize ) + '。';
  333. } else if ( state === 'confirm' ) {
  334. stats = uploader.getStats();
  335. if ( stats.uploadFailNum ) {
  336. text = '已成功上传' + stats.successNum+ '张照片至XX相册,'+
  337. stats.uploadFailNum + '张照片上传失败,<a class="retry" href="#">重新上传</a>失败图片或<a class="ignore" href="#">忽略</a>'
  338. }
  339. } else {
  340. stats = uploader.getStats();
  341. text = '共' + fileCount + '张(' +
  342. WebUploader.formatSize( fileSize ) +
  343. '),已上传' + stats.successNum + '张';
  344. if ( stats.uploadFailNum ) {
  345. text += ',失败' + stats.uploadFailNum + '张';
  346. }
  347. }
  348. $info.html( text );
  349. }
  350. function setState( val ) {
  351. var file, stats;
  352. if ( val === state ) {
  353. return;
  354. }
  355. $upload.removeClass( 'state-' + state );
  356. $upload.addClass( 'state-' + val );
  357. state = val;
  358. switch ( state ) {
  359. case 'pedding':
  360. $placeHolder.removeClass( 'element-invisible' );
  361. $queue.hide();
  362. $statusBar.addClass( 'element-invisible' );
  363. uploader.refresh();
  364. break;
  365. case 'ready':
  366. $placeHolder.addClass( 'element-invisible' );
  367. $( '#filePicker2' ).removeClass( 'element-invisible');
  368. $queue.show();
  369. $statusBar.removeClass('element-invisible');
  370. uploader.refresh();
  371. break;
  372. case 'uploading':
  373. $( '#filePicker2' ).addClass( 'element-invisible' );
  374. $progress.show();
  375. $upload.text( '暂停上传' );
  376. break;
  377. case 'paused':
  378. $progress.show();
  379. $upload.text( '继续上传' );
  380. break;
  381. case 'confirm':
  382. $progress.hide();
  383. $( '#filePicker2' ).removeClass( 'element-invisible' );
  384. $upload.text( '开始上传' );
  385. stats = uploader.getStats();
  386. if ( stats.successNum && !stats.uploadFailNum ) {
  387. setState( 'finish' );
  388. return;
  389. }
  390. break;
  391. case 'finish':
  392. stats = uploader.getStats();
  393. if ( stats.successNum ) {
  394. alert( '上传成功' );
  395. } else {
  396. // 没有成功的图片,重设
  397. state = 'done';
  398. location.reload();
  399. }
  400. break;
  401. }
  402. updateStatus();
  403. }
  404. uploader.onUploadProgress = function( file, percentage ) {
  405. var $li = $('#'+file.id),
  406. $percent = $li.find('.progress span');
  407. $percent.css( 'width', percentage * 100 + '%' );
  408. percentages[ file.id ][ 1 ] = percentage;
  409. updateTotalProgress();
  410. };
  411. uploader.onFileQueued = function( file ) {
  412. fileCount++;
  413. fileSize += file.size;
  414. if ( fileCount === 1 ) {
  415. $placeHolder.addClass( 'element-invisible' );
  416. $statusBar.show();
  417. }
  418. addFile( file );
  419. setState( 'ready' );
  420. updateTotalProgress();
  421. };
  422. uploader.onFileDequeued = function( file ) {
  423. fileCount--;
  424. fileSize -= file.size;
  425. if ( !fileCount ) {
  426. setState( 'pedding' );
  427. }
  428. removeFile( file );
  429. updateTotalProgress();
  430. };
  431. uploader.on( 'all', function( type ) {
  432. var stats;
  433. switch( type ) {
  434. case 'uploadFinished':
  435. setState( 'confirm' );
  436. break;
  437. case 'startUpload':
  438. setState( 'uploading' );
  439. break;
  440. case 'stopUpload':
  441. setState( 'paused' );
  442. break;
  443. }
  444. });
  445. uploader.onError = function( code ) {
  446. alert( 'Eroor: ' + code );
  447. };
  448. $upload.on('click', function() {
  449. if ( $(this).hasClass( 'disabled' ) ) {
  450. return false;
  451. }
  452. if ( state === 'ready' ) {
  453. uploader.upload();
  454. } else if ( state === 'paused' ) {
  455. uploader.upload();
  456. } else if ( state === 'uploading' ) {
  457. uploader.stop();
  458. }
  459. });
  460. $info.on( 'click', '.retry', function() {
  461. uploader.retry();
  462. } );
  463. $info.on( 'click', '.ignore', function() {
  464. alert( 'todo' );
  465. } );
  466. $upload.addClass( 'state-' + state );
  467. updateTotalProgress();
  468. });
  469. })( jQuery );