yvanui.upload.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /**
  2. * YvanUI
  3. *
  4. * Copyright (c) 2019 www.yvanui.com. All rights reserved.
  5. * @author luoyifan@qq.com
  6. * @time 2019-03-07 18:56:00
  7. */
  8. (function ($) {
  9. if (!WebUploader) {
  10. console.error('请引用 WebUploader 组件');
  11. return;
  12. }
  13. var uploadWindow = function (context) {
  14. var uploader;
  15. var inputId = $.yvan.createId('inputFile');
  16. var dndId = $.yvan.createId('dnd');
  17. var $dg, $footer;
  18. function onOpenFunction() {
  19. var refreshStatus = function () {
  20. var s = uploader.getStats();
  21. var t = [];
  22. if (s.successNum) {
  23. t.push('成功' + s.successNum + "个");
  24. }
  25. if (s.invalidNum) {
  26. t.push('无效' + s.invalidNum + "个");
  27. }
  28. if (s.uploadFailNum) {
  29. t.push('失败' + s.uploadFailNum + "个");
  30. }
  31. if (s.queueNum) {
  32. t.push('等待' + s.queueNum + "个");
  33. }
  34. // if (t.length <= 0) {
  35. // $footer.html('&nbsp;');
  36. // } else {
  37. // $footer.html(t.join(" / "));
  38. // }
  39. };
  40. uploader = WebUploader.create({
  41. swf: '/static/plugins/webuploader-0.1.5/Uploader.swf',
  42. server: context.url,
  43. formData: {
  44. Authorization: $.cookie('auth'),
  45. param: context.param
  46. },
  47. pick: {
  48. id: '#' + inputId,
  49. label: '点击上传'
  50. },
  51. dnd: '#' + dndId,
  52. paste: document.body,
  53. resize: false,
  54. accept: context.accept,
  55. //chunked: true,
  56. //chunkSize: 1 * 1024 * 1024,
  57. chunked: false,
  58. threads: 2,
  59. auto: true,
  60. disableGlobalDnd: true,
  61. //fileNumLimit: 10,
  62. //fileSingleSizeLimit: 300 * 1024 * 1024 // 100 M
  63. });
  64. // 当有文件被添加进队列的时候
  65. uploader.on('fileQueued', function (file) {
  66. $dg.datagrid('appendRow', {
  67. id: file.id, filename: file.name, size: file.size, ext: file.ext, progress: '准备上传'
  68. });
  69. refreshStatus();
  70. file.on('statuschange', function (cur, prev, a, b, c) {
  71. refreshStatus();
  72. var index = $dg.datagrid('getRowIndex', file.id);
  73. if (index < 0) {
  74. return;
  75. }
  76. var row = {
  77. id: file.id, filename: file.name, size: file.size, ext: file.ext, progress: '正在上传'
  78. };
  79. //$footer.html('ccc');
  80. // 成功
  81. if (cur === 'error' || cur === 'invalid') {
  82. row.progress = '上传错误';
  83. } else if (cur === 'interrupt') {
  84. row.progress = '上传被终止';
  85. } else if (cur === 'queued') {
  86. } else if (cur === 'progress') {
  87. } else if (cur === 'complete') {
  88. row.progress = '上传完毕';
  89. if ($.type(context.uploadSuccess) === 'function') {
  90. context.uploadSuccess.call(this, file);
  91. }
  92. } else {
  93. }
  94. $dg.datagrid('updateRow', {index: index, row: row});
  95. });
  96. });
  97. // 文件上传过程中创建进度条实时显示。
  98. uploader.on('uploadProgress', function (file, percentage) {
  99. refreshStatus();
  100. //getRowIndex
  101. var index = $dg.datagrid('getRowIndex', file.id);
  102. if (index < 0) {
  103. return;
  104. }
  105. $dg.datagrid('updateRow', {
  106. index: index,
  107. row: {
  108. id: file.id, filename: file.name, size: file.size, ext: file.ext, progress: percentage * 100
  109. }
  110. });
  111. });
  112. // 上传完成,无论如何都要执行该方法
  113. uploader.on('uploadSuccess', function (file, response) {
  114. if (response.success == false) {
  115. var index = $dg.datagrid('getRowIndex', file.id);
  116. if (index < 0) {
  117. return;
  118. }
  119. var row = {
  120. id: file.id, filename: file.name, size: file.size, ext: file.ext, progress: '<span style="color: red">上传失败</span>'
  121. };
  122. $dg.datagrid('updateRow', {index: index, row: row});
  123. $footer.html("");
  124. $.yvan.msg(response.msg);
  125. return;
  126. }
  127. if ($.type(context.success) === 'function') {
  128. context.success.call(this, response);
  129. }
  130. });
  131. }
  132. return {
  133. xtype: 'dialog',
  134. dialogId: $.yvan.createId('dialog'),
  135. title: '文件批量上传',
  136. width: 600,
  137. height: 300,
  138. modal: false,
  139. onOpen: function () {
  140. setTimeout(onOpenFunction, 0);
  141. },
  142. toolbar:
  143. '<div class="uploader">\n' +
  144. ' <div id="' + dndId + '" class="queueList">\n' +
  145. ' <div id="dndArea" class="placeholder">\n' +
  146. ' <div id="' + inputId + '"></div>\n' +
  147. ' <p>或将文件(文件夹)拖到这里</p>\n' +
  148. ' </div>\n' +
  149. ' </div>\n' +
  150. '</div>',
  151. center: {
  152. border: false,
  153. items: {
  154. xtype: 'datagrid',
  155. onRender: function () {
  156. $dg = $(this);
  157. },
  158. pagination: false,
  159. singleSelect: true,
  160. striped: true,
  161. fitColumns: true,
  162. rownumbers: false,
  163. showHeader: false,
  164. emptyMsg: '',
  165. idField: 'id',
  166. columns: [[
  167. {field: 'id', hidden: true},
  168. {field: 'size', hidden: true},
  169. {field: 'ext', hidden: true},
  170. {field: 'filename', width: 250},
  171. {
  172. field: 'progress', width: 50, formatter: function (value) {
  173. if (!value) {
  174. value = '0';
  175. }
  176. var re = /^[0-9]+.?[0-9]*$/;
  177. if (re.test(value)) {
  178. value = parseInt(value);
  179. if (!value || value < 0) {
  180. value = 0;
  181. }
  182. if (value > 100) {
  183. value = 100;
  184. }
  185. return '<div class="progress">' +
  186. '<div class="progress-bar" role="progressbar" aria-valuenow="60" aria-valuemin="0" aria-valuemax="100" style="width: ' + value + '%;">' +
  187. value + '%</div>' +
  188. '</div>';
  189. }
  190. if (value.indexOf('错误') > -1) {
  191. return '<span style="color:red">' + value + '</span>';
  192. }
  193. return value;
  194. }
  195. },
  196. ]],
  197. data: []
  198. }
  199. },
  200. footer: {
  201. xtype: 'div',
  202. onRender: function () {
  203. $footer = $(this);
  204. },
  205. items: [
  206. '&nbsp;'
  207. ]
  208. }
  209. };
  210. };
  211. $.extend($.yvan, {
  212. showUploadWindow: function ($sender, opts) {
  213. opts = uploadWindow(opts);
  214. return $.yvan.showDialog($sender, opts);
  215. }
  216. });
  217. })(jQuery);