uploader.js 7.0 KB

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