multiupload.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  1. if (!window.UserControl) window.UserControl = {};
  2. UserControl.MultiUpload = function () {
  3. UserControl.MultiUpload.superclass.constructor.call(this);
  4. var me = this;
  5. setTimeout(function () {
  6. // me.initComponents();
  7. me.bindEvents();
  8. }, 300);
  9. }
  10. mini.extend(UserControl.MultiUpload, mini.DataGrid, {
  11. uiCls: 'uc-multiupload',
  12. flashUrl: '',
  13. uploadUrl: '',
  14. uploader: undefined,
  15. uploadName: "Fdata",
  16. limitSize: "500MB",
  17. limitType: "*",
  18. uploadLimit: 0,
  19. queueLimit: 30,
  20. postParam: {},
  21. // continuity: false, //连续上传
  22. autoUpload: false, //选中即上传
  23. customSettings: { queue: null },
  24. columnsTexts: {
  25. nameColumnHeader: "文件名",
  26. typeColumnHeader: "文件类型",
  27. sizeColumnHeader: "文件大小",
  28. completeColumnHeader: "上传进度",
  29. statusColumnHeader: "上传状态",
  30. reasonColumnHeader: "原因",
  31. actionColumnHeader: "操作"
  32. },
  33. _create: function () {
  34. UserControl.MultiUpload.superclass._create.call(this);
  35. var me = this;
  36. var defaultColumns = [
  37. { "type": "indexColumn" },
  38. { field: "name", width: 200, header: me.columnsTexts.nameColumnHeader },
  39. { field: "type", width: 80, header: me.columnsTexts.typeColumnHeader, align: "center", headerAlign: "center" },
  40. { field: "size", width: 80, header: me.columnsTexts.sizeColumnHeader, align: "center", headerAlign: "center" },
  41. { field: "complete", width: 80, header: me.columnsTexts.completeColumnHeader, headerAlign: "center" },
  42. { field: "status", width: 80, header: me.columnsTexts.statusColumnHeader, align: "center", headerAlign: "center" },
  43. { field: "reason", width: 250, header: me.columnsTexts.reasonColumnHeader, align: "left", headerAlign: "center" },
  44. { field: "action", width: 50, header: me.columnsTexts.actionColumnHeader, align: "center", headerAlign: "center" }
  45. ];
  46. me.set({
  47. showPager: false,
  48. showToolbar: true,
  49. columns: defaultColumns
  50. })
  51. var toolbarEl = me.getToolbarEl();
  52. toolbarEl.style.height = "35px";
  53. me._uploadId = me._id + "_button_placeholder";
  54. var sb = [];
  55. sb.push('<table><tr><td style="width:120px;height:25px;"><a class="mini-button" iconCls="icon-search" style="width:120px">选择文件...</a><span class="mini-upload"><span id="' + me._uploadId + '" class="mini-upload-placeholder" style="height:28px;"></span></span>');
  56. sb.push('</td><td><a class="mini-button" iconCls="icon-upload" name="multiupload">开始上传</a>');
  57. sb.push('</td><td><a class="mini-button" iconCls="icon-remove" name="removeAll">清空上传</a>');
  58. sb.push('</td></tr></table>');
  59. toolbarEl.innerHTML = sb.join("");
  60. },
  61. // initComponents: function () {
  62. // },
  63. __OnMouseMove: function () {
  64. var me = this;
  65. if (!me.uploader) {
  66. var limitTypes = me.limitType.split(";");
  67. var lt = [];
  68. var et = [];
  69. for (var i = 0, l = limitTypes.length; i < l; i++) {
  70. var item = limitTypes[i];
  71. et.push(item.replace("*.", ""));
  72. for (var j = 0, k = me.acceptMap.length; j < k; j++) {
  73. if (me.acceptMap[j].code == item) {
  74. lt.push(me.acceptMap[j].text);
  75. }
  76. }
  77. }
  78. var limitType = lt.join(",");
  79. var limitSize = me.changeByte(me.limitSize);
  80. if (limitSize == 0) {
  81. limitSize = 100*1024*1024;
  82. }
  83. var upload = WebUploader.create({
  84. server: me.uploadUrl,
  85. pick: "#" + me._uploadId,
  86. auto: me.autoUpload,
  87. accept: {
  88. extensions: et.join(","),
  89. mimeTypes: "*/*"
  90. },
  91. formData: me.postParam,
  92. // 文件上传参数表,用来标记文件的所有者(如果是一篇文章的附件,就是文章的ID)
  93. formData: {
  94. owner: 'webuploader.webuploader'
  95. },
  96. fileVal: me.uploadName,
  97. // 单个文件大小限制(单位:byte),这里限制为 100M
  98. fileSingleSizeLimit: limitSize,
  99. fileNumLimit: me.queueLimit
  100. })
  101. upload.on("fileQueued", function (file) {
  102. me.__on_file_queued(file, me);
  103. })
  104. upload.on("error", function (type) {
  105. me.__on_Error(type, me);
  106. })
  107. upload.on('uploadProgress', function (file, percentage) {
  108. me.__on_upload_progress(file, percentage, me);
  109. });
  110. upload.on('uploadSuccess', function (file, response) {
  111. if (response.res == "0") {
  112. me.__on_upload_error(file, response.msg, me);
  113. } else {
  114. me.__on_upload_success(file, response, me);
  115. }
  116. });
  117. upload.on('uploadError', function (file, reason) {
  118. me.__on_upload_error(file, reason, me);
  119. });
  120. upload.on('uploadBeforeSend', function (file) {
  121. /*me.extend(headers, {
  122. "Origin":"http://192.168.8.103:913",
  123. "Access-Control-Request-Method": "POST"
  124. });*/
  125. });
  126. // 不管上传成功还是失败,都会触发 uploadComplete 事件
  127. upload.on('uploadComplete', function (file) {
  128. me.__on_upload_complete(file, me);
  129. });
  130. // 当开始上传流程时触发
  131. upload.on('startUpload', function () {
  132. // mini.get("beginBtn").setEnabled(false);
  133. });
  134. // 当所有文件上传结束时触发
  135. upload.on('uploadFinished', function () {
  136. // mini.get("beginBtn").setEnabled(true);
  137. });
  138. me.uploader = upload;
  139. me.uploadButton.on("click", function () {
  140. var rows = me.getData();
  141. if (rows.length > 0) {
  142. me.continuity = true;
  143. me.startUpload();
  144. }
  145. });
  146. me.removeButton.on("click", function () {
  147. window.location = window.location.href;
  148. /*
  149. var rows = me.getData();
  150. for (var i = 0, l = rows.length; i < l; i++) {
  151. me.uploader.cancelFile(rows[i].fileId);
  152. me.uploader.removeFile(rows[i].fileId);
  153. }
  154. me.clearData();*/
  155. });
  156. }
  157. },
  158. bindEvents: function () {
  159. var me = this;
  160. me._fileEl = document.getElementById(me._uploadId);
  161. me.uploadEl = me._fileEl;
  162. var toolbarEl = me.getToolbarEl();
  163. mini.on(me.uploadEl, "mousemove", me.__OnMouseMove, me);
  164. me.uploadButton = mini.getbyName("multiupload", toolbarEl);
  165. me.removeButton = mini.getbyName("removeAll", toolbarEl);
  166. me.on("drawcell", function (e) {
  167. var field = e.field;
  168. var record = e.record;
  169. var uid = record._uid;
  170. var value = e.value;
  171. // if (field == "size") {
  172. // e.cellHtml = bytesToSize(e.value);
  173. // }
  174. if (field == "complete") {
  175. e.cellHtml = '<div class="progressbar">'
  176. + '<div class="progressbar-percent" style="width:' + value + '%;"></div>'
  177. + '<div class="progressbar-label">' + value + '%</div>'
  178. + '</div>';
  179. }
  180. if (field == "status") {
  181. if (e.value == 0) {
  182. e.cellHtml = "准备上传";
  183. } else if (e.value == 1) {
  184. e.cellHtml = "上传成功";
  185. } else if (e.value == 2) {
  186. e.cellHtml = "上传失败";
  187. }
  188. }
  189. if (field == "action") {
  190. e.cellHtml = '<a class="upicon-remove" name="' + uid + '"><a>';
  191. }
  192. })
  193. $(document.body).on("click", ".upicon-remove", function () {
  194. var uid = $(this).attr("name");
  195. var row = me.getRowByUID(uid);
  196. if (me.uploader.getStats().queueNum !== 0) {
  197. me.uploader.cancelFile(row.fileId);
  198. me.uploader.removeFile(row.fileId);
  199. }
  200. me.removeRow(row);
  201. })
  202. },
  203. startUpload: function (fileId) {
  204. var me = this;
  205. if (me.uploader) {
  206. // if (me.postParam) {
  207. // me.uploader.setPostParams(this.postParam);
  208. // }
  209. if (fileId) {
  210. me.uploader.upload(fileId);
  211. } else {
  212. me.uploader.upload();
  213. }
  214. }
  215. },
  216. addPostParam: function (value) {
  217. mini.copyTo(this.postParam, value);
  218. if (this.uploader) {
  219. this.uploader.option("formData", this.postParam);
  220. }
  221. },
  222. setPostParam: function (value) {
  223. this.addPostParam(value);
  224. },
  225. getPostParam: function () {
  226. return this.postParam;
  227. },
  228. __on_file_queued: function (file, me) {
  229. function bytesToSize(bytes) {
  230. if (bytes === 0) return '0 B';
  231. var k = 1024,
  232. sizes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'],
  233. i = Math.floor(Math.log(bytes) / Math.log(k));
  234. return (bytes / Math.pow(k, i)).toPrecision(3) + ' ' + sizes[i];
  235. }
  236. var me = this;
  237. var row = {};
  238. row.name = file.name;
  239. row.type = file.type;
  240. row.status = 0;
  241. row.fileId = file.id;
  242. row.size = bytesToSize(file.size);
  243. row.complete = 0;
  244. me.addRow(row);
  245. if (me.autoUpload) {
  246. me.startUpload(file.id);
  247. }
  248. },
  249. __on_upload_error: function (file, reason, me) {
  250. if (reason == "File Cancelled") return;
  251. if (file) {
  252. var row = me.findRow(function (row) {
  253. if (row.fileId == file.id) return true;
  254. })
  255. me.updateRow(row, { status: 2,reason:reason });
  256. }
  257. var e = { file: file, code: reason, message: reason };
  258. me.fire("uploaderror", e);
  259. },
  260. __on_upload_success: function (file, response, me) {
  261. var row = me.findRow(function (row) {
  262. if (row.fileId == file.id) return true;
  263. })
  264. me.updateRow(row, { status: 1, complete: 100 });
  265. var e = { file: file, serverData: response._raw };
  266. me.fire("uploadsuccess", e);
  267. },
  268. __on_upload_complete: function (file, me) {
  269. if (me.uploader.getStats().queueNum !== 0) {
  270. me.startUpload();
  271. }
  272. // }
  273. },
  274. __on_Error: function (type, me) {
  275. var msg = "";
  276. if (type == "F_DUPLICATE") msg = "已存在同名文件";
  277. else msg = type;
  278. mini.alert("文件选择出错!" + msg);
  279. },
  280. __on_upload_progress: function (file, percentage, me) {
  281. var row = me.findRow(function (row) {
  282. if (row.fileId == file.id) return true;
  283. })
  284. me.updateRow(row, { complete: percentage });
  285. },
  286. // __on_file_queued_error: function (file, errorCode, msg) {
  287. // mini.alert("文件选择出错!errorCode:" + errorCode + ";msg:" + msg);
  288. // },
  289. setUploadUrl: function (url) {
  290. this.uploadUrl = url;
  291. },
  292. getUploadUrl: function () {
  293. return this.uploadUrl;
  294. },
  295. setFlashUrl: function (url) {
  296. this.flashUrl = url;
  297. },
  298. getFlashUrl: function () {
  299. return this.flashUrl;
  300. },
  301. setLimitType: function (type) {
  302. this.limitType = type;
  303. },
  304. getLimitType: function () {
  305. return this.limitType;
  306. },
  307. setLimitSize: function (size) {
  308. this.limitSize = size;
  309. },
  310. getLimitTSize: function () {
  311. return this.limitSize;
  312. },
  313. setUploadName: function (name) {
  314. this.uploadName = name;
  315. },
  316. getUploadName: function () {
  317. return this.uploadName;
  318. },
  319. setAutoUpload: function (val) {
  320. this.autoUpload = val;
  321. },
  322. getAutoUpload: function () {
  323. return this.autoUpload;
  324. },
  325. setQueueLimit: function (num) {
  326. this.queueLimit = num;
  327. },
  328. getQueueLimit: function () {
  329. return this.queueLimit;
  330. },
  331. getAttrs: function (el) {
  332. var attrs = UserControl.MultiUpload.superclass.getAttrs.call(this, el);
  333. mini._ParseString(el, attrs,
  334. ["uploadUrl", "flashUrl", "limitType", "limitSize", "uploadName", "queueLimit", "onuploaderror", "onuploadsuccess"]
  335. );
  336. mini._ParseBool(el, attrs,
  337. ["autoUpload"]
  338. );
  339. return attrs;
  340. },
  341. acceptMap: [
  342. { code: "*.3gpp", text: "audio/3gpp" },
  343. { code: "*.ac3", text: "audio/ac3" },
  344. { code: "*.asf", text: "allpication/vnd.ms-asf" },
  345. { code: "*.au", text: "audio/basic" },
  346. { code: "*.css", text: "text/css" },
  347. { code: "*.csv", text: "text/csv" },
  348. { code: "*.cdr", text: "image/cdr" },
  349. { code: "*.rar", text: "application/octet-stream" },
  350. { code: "*.doc", text: "application/msword" },
  351. { code: "*.dot", text: "application/msword" },
  352. { code: "*.dtd", text: "application/xml-dtd" },
  353. { code: "*.dwg", text: "image/vnd.dwg" },
  354. { code: "*.dxf", text: "image/vnd.dxf" },
  355. { code: "*.gif", text: "image/gif" },
  356. { code: "*.htm", text: "text/html" },
  357. { code: "*.html", text: "text/html" },
  358. { code: "*.jp2", text: "image/jp2" },
  359. { code: "*.jpe", text: "image/jpeg" },
  360. { code: "*.jpeg", text: "image/jpeg" },
  361. { code: "*.jpg", text: "image/jpeg" },
  362. { code: "*.js", text: "text/javascript" },
  363. { code: "*.json", text: "application/json" },
  364. { code: "*.mp2", text: "audio/mpeg" },
  365. { code: "*.mp3", text: "audio/mpeg" },
  366. { code: "*.mp4", text: "audio/mp4" },
  367. { code: "*.mpeg", text: "video/mpeg" },
  368. { code: "*.mpg", text: "video/mpeg" },
  369. { code: "*.mpp", text: "application/vnd.ms-project" },
  370. { code: "*.ogg", text: "audio/ogg" },
  371. { code: "*.pdf", text: "application/pdf" },
  372. { code: "*.png", text: "image/png" },
  373. { code: "*.pot", text: "application/vnd.ms-powerpoint" },
  374. { code: "*.pps", text: "application/vnd.ms-powerpoint" },
  375. { code: "*.ppt", text: "application/vnd.ms-powerpoint" },
  376. { code: "*.rtf", text: "text/rtf" },
  377. { code: "*.svf", text: "image/vnd.svf" },
  378. { code: "*.tif", text: "image/tiff" },
  379. { code: "*.tiff", text: "image/tiff" },
  380. { code: "*.txt", text: "text/plain" },
  381. { code: "*.wdb", text: "application/vnd.ms-works" },
  382. { code: "*.wps", text: "application/vnd.ms-works" },
  383. { code: "*.xhtml", text: "application/xhtml+xml" },
  384. { code: "*.xlc", text: "application/vnd.ms-excel" },
  385. { code: "*.xlm", text: "application/vnd.ms-excel" },
  386. { code: "*.xls", text: "application/vnd.ms-excel" },
  387. { code: "*.xlt", text: "application/vnd.ms-excel" },
  388. { code: "*.xlw", text: "application/vnd.ms-excel" },
  389. { code: "*.xml", text: "text/xml" },
  390. { code: "*.zip", text: "application/zip" },
  391. { code: "*.xlsx", text: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" },
  392. { code: "*.xltx", text: "application/vnd.openxmlformats-officedocument.spreadsheetml.template" },
  393. { code: "*.potx", text: "application/vnd.openxmlformats-officedocument.presentationml.template" },
  394. { code: "*.ppsx", text: "application/vnd.openxmlformats-officedocument.presentationml.slideshow" },
  395. { code: "*.pptx", text: "application/vnd.openxmlformats-officedocument.presentationml.presentation" },
  396. { code: "*.sldx", text: "application/vnd.openxmlformats-officedocument.presentationml.slide" },
  397. { code: "*.docx", text: "application/vnd.openxmlformats-officedocument.wordprocessingml.document" },
  398. { code: "*.dotx", text: "application/vnd.openxmlformats-officedocument.wordprocessingml.template" },
  399. { code: "*.xlsm", text: "application/vnd.ms-excel.addin.macroEnabled.12" },
  400. { code: "*.xlsb", text: " application/vnd.ms-excel.sheet.binary.macroEnabled.12" }
  401. ],
  402. changeByte: function (value) {
  403. var number = parseInt(value);
  404. if (isNaN(number) || number == 0) {
  405. return 0;
  406. }
  407. var l2 = String(number).length;
  408. var unit = value.substring(l2).toLowerCase();
  409. var result = number;
  410. if (unit == "kb") {
  411. result = result * 1024;
  412. } else if (unit == "mb") {
  413. result = result * 1024 * 1024;
  414. } else if (unit == "gb" || unit == "g") {
  415. result = result * 1024 * 1024 * 1024;
  416. }
  417. return result;
  418. }
  419. });
  420. mini.regClass(UserControl.MultiUpload, "multiupload");