uploader.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. (function (root, factory) {
  2. if (typeof define === 'function' && define.amd) {
  3. // AMD. Register as an anonymous module unless amdModuleId is set
  4. define('simple-uploader', ["jquery","simple-module"], function ($, SimpleModule) {
  5. return (root['uploader'] = factory($, SimpleModule));
  6. });
  7. } else if (typeof exports === 'object') {
  8. // Node. Does not work with strict CommonJS, but
  9. // only CommonJS-like environments that support module.exports,
  10. // like Node.
  11. module.exports = factory(require("jquery"),require("simple-module"));
  12. } else {
  13. root.simple = root.simple || {};
  14. root.simple['uploader'] = factory(jQuery,SimpleModule);
  15. }
  16. }(this, function ($, SimpleModule) {
  17. var Uploader, uploader,
  18. extend = function(child, parent) { for (var key in parent) { if (hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; },
  19. hasProp = {}.hasOwnProperty;
  20. Uploader = (function(superClass) {
  21. extend(Uploader, superClass);
  22. function Uploader() {
  23. return Uploader.__super__.constructor.apply(this, arguments);
  24. }
  25. Uploader.count = 0;
  26. Uploader.prototype.opts = {
  27. url: '',
  28. params: null,
  29. fileKey: 'upload_file',
  30. connectionCount: 3
  31. };
  32. Uploader.prototype._init = function() {
  33. this.files = [];
  34. this.queue = [];
  35. this.id = ++Uploader.count;
  36. this.on('uploadcomplete', (function(_this) {
  37. return function(e, file) {
  38. _this.files.splice($.inArray(file, _this.files), 1);
  39. if (_this.queue.length > 0 && _this.files.length < _this.opts.connectionCount) {
  40. return _this.upload(_this.queue.shift());
  41. } else {
  42. return _this.uploading = false;
  43. }
  44. };
  45. })(this));
  46. return $(window).on('beforeunload.uploader-' + this.id, (function(_this) {
  47. return function(e) {
  48. if (!_this.uploading) {
  49. return;
  50. }
  51. e.originalEvent.returnValue = _this._t('leaveConfirm');
  52. return _this._t('leaveConfirm');
  53. };
  54. })(this));
  55. };
  56. Uploader.prototype.generateId = (function() {
  57. var id;
  58. id = 0;
  59. return function() {
  60. return id += 1;
  61. };
  62. })();
  63. Uploader.prototype.upload = function(file, opts) {
  64. var f, i, key, len;
  65. if (opts == null) {
  66. opts = {};
  67. }
  68. if (file == null) {
  69. return;
  70. }
  71. if ($.isArray(file) || file instanceof FileList) {
  72. for (i = 0, len = file.length; i < len; i++) {
  73. f = file[i];
  74. this.upload(f, opts);
  75. }
  76. } else if ($(file).is('input:file')) {
  77. key = $(file).attr('name');
  78. if (key) {
  79. opts.fileKey = key;
  80. }
  81. this.upload($.makeArray($(file)[0].files), opts);
  82. } else if (!file.id || !file.obj) {
  83. file = this.getFile(file);
  84. }
  85. if (!(file && file.obj)) {
  86. return;
  87. }
  88. $.extend(file, opts);
  89. if (this.files.length >= this.opts.connectionCount) {
  90. this.queue.push(file);
  91. return;
  92. }
  93. if (this.triggerHandler('beforeupload', [file]) === false) {
  94. return;
  95. }
  96. this.files.push(file);
  97. this._xhrUpload(file);
  98. return this.uploading = true;
  99. };
  100. Uploader.prototype.getFile = function(fileObj) {
  101. var name, ref, ref1;
  102. if (fileObj instanceof window.File || fileObj instanceof window.Blob) {
  103. name = (ref = fileObj.fileName) != null ? ref : fileObj.name;
  104. } else {
  105. return null;
  106. }
  107. return {
  108. id: this.generateId(),
  109. url: this.opts.url,
  110. params: this.opts.params,
  111. fileKey: this.opts.fileKey,
  112. name: name,
  113. size: (ref1 = fileObj.fileSize) != null ? ref1 : fileObj.size,
  114. ext: name ? name.split('.').pop().toLowerCase() : '',
  115. obj: fileObj
  116. };
  117. };
  118. Uploader.prototype._xhrUpload = function(file) {
  119. var formData, k, ref, v;
  120. formData = new FormData();
  121. formData.append(file.fileKey, file.obj);
  122. formData.append("original_filename", file.name);
  123. if (file.params) {
  124. ref = file.params;
  125. for (k in ref) {
  126. v = ref[k];
  127. formData.append(k, v);
  128. }
  129. }
  130. return file.xhr = $.ajax({
  131. url: file.url,
  132. data: formData,
  133. processData: false,
  134. contentType: false,
  135. type: 'POST',
  136. headers: {
  137. 'X-File-Name': encodeURIComponent(file.name)
  138. },
  139. xhr: function() {
  140. var req;
  141. req = $.ajaxSettings.xhr();
  142. if (req) {
  143. req.upload.onprogress = (function(_this) {
  144. return function(e) {
  145. return _this.progress(e);
  146. };
  147. })(this);
  148. }
  149. return req;
  150. },
  151. progress: (function(_this) {
  152. return function(e) {
  153. if (!e.lengthComputable) {
  154. return;
  155. }
  156. return _this.trigger('uploadprogress', [file, e.loaded, e.total]);
  157. };
  158. })(this),
  159. error: (function(_this) {
  160. return function(xhr, status, err) {
  161. return _this.trigger('uploaderror', [file, xhr, status]);
  162. };
  163. })(this),
  164. success: (function(_this) {
  165. return function(result) {
  166. _this.trigger('uploadprogress', [file, file.size, file.size]);
  167. _this.trigger('uploadsuccess', [file, result]);
  168. return $(document).trigger('uploadsuccess', [file, result, _this]);
  169. };
  170. })(this),
  171. complete: (function(_this) {
  172. return function(xhr, status) {
  173. return _this.trigger('uploadcomplete', [file, xhr.responseText]);
  174. };
  175. })(this)
  176. });
  177. };
  178. Uploader.prototype.cancel = function(file) {
  179. var f, i, len, ref;
  180. if (!file.id) {
  181. ref = this.files;
  182. for (i = 0, len = ref.length; i < len; i++) {
  183. f = ref[i];
  184. if (f.id === file * 1) {
  185. file = f;
  186. break;
  187. }
  188. }
  189. }
  190. this.trigger('uploadcancel', [file]);
  191. if (file.xhr) {
  192. file.xhr.abort();
  193. }
  194. return file.xhr = null;
  195. };
  196. Uploader.prototype.readImageFile = function(fileObj, callback) {
  197. var fileReader, img;
  198. if (!$.isFunction(callback)) {
  199. return;
  200. }
  201. img = new Image();
  202. img.onload = function() {
  203. return callback(img);
  204. };
  205. img.onerror = function() {
  206. return callback();
  207. };
  208. if (window.FileReader && FileReader.prototype.readAsDataURL && /^image/.test(fileObj.type)) {
  209. fileReader = new FileReader();
  210. fileReader.onload = function(e) {
  211. return img.src = e.target.result;
  212. };
  213. return fileReader.readAsDataURL(fileObj);
  214. } else {
  215. return callback();
  216. }
  217. };
  218. Uploader.prototype.destroy = function() {
  219. var file, i, len, ref;
  220. this.queue.length = 0;
  221. ref = this.files;
  222. for (i = 0, len = ref.length; i < len; i++) {
  223. file = ref[i];
  224. this.cancel(file);
  225. }
  226. $(window).off('.uploader-' + this.id);
  227. return $(document).off('.uploader-' + this.id);
  228. };
  229. Uploader.i18n = {
  230. 'zh-CN': {
  231. leaveConfirm: '正在上传文件,如果离开上传会自动取消'
  232. }
  233. };
  234. Uploader.locale = 'zh-CN';
  235. return Uploader;
  236. })(SimpleModule);
  237. uploader = function(opts) {
  238. return new Uploader(opts);
  239. };
  240. return uploader;
  241. }));