bootstrap.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. //修改上传的接口调用
  2. require(['upload'], function (Upload) {
  3. var _onInit = Upload.events.onInit;
  4. //初始化中完成判断
  5. Upload.events.onInit = function () {
  6. _onInit.apply(this, Array.prototype.slice.apply(arguments));
  7. //如果上传接口不是七牛云,则不处理
  8. if (this.options.url !== Config.upload.uploadurl) {
  9. return;
  10. }
  11. var _success = this.options.success;
  12. $.extend(this.options, {
  13. //关闭自动处理队列功能
  14. autoQueue: false,
  15. chunkSuccess: function (chunk, file, response) {
  16. this.contexts = this.contexts ? this.contexts : [];
  17. this.contexts.push(typeof response.ctx !== 'undefined' ? response.ctx : response.data.ctx);
  18. },
  19. chunksUploaded: function (file, done) {
  20. var that = this;
  21. var params = $(that.element).data("params") || {};
  22. var category = typeof params.category !== 'undefined' ? params.category : ($(that.element).data("category") || '');
  23. category = typeof category === 'function' ? category.call(this, file) : category;
  24. Fast.api.ajax({
  25. url: "/addons/qiniu/index/upload",
  26. data: {
  27. action: 'merge',
  28. filesize: file.size,
  29. filename: file.name,
  30. chunkid: file.upload.uuid,
  31. chunkcount: file.upload.totalChunkCount,
  32. width: file.width || 0,
  33. height: file.height || 0,
  34. type: file.type,
  35. category: category,
  36. qiniutoken: Config.upload.multipart.qiniutoken,
  37. contexts: this.contexts
  38. },
  39. }, function (data, ret) {
  40. done(JSON.stringify(ret));
  41. return false;
  42. }, function (data, ret) {
  43. file.accepted = false;
  44. that._errorProcessing([file], ret.msg);
  45. return false;
  46. });
  47. },
  48. });
  49. //先移除已有的事件
  50. this.off("success", _success).on("success", function (file, response) {
  51. var that = this;
  52. var ret = {code: 0, msg: response};
  53. try {
  54. ret = typeof response === 'string' ? JSON.parse(response) : response;
  55. if (file.xhr.status === 200) {
  56. if (Config.upload.uploadmode === 'client') {
  57. if (typeof ret.key !== 'undefined') {
  58. ret = {code: 1, msg: "", data: {url: '/' + ret.key, hash: ret.hash}};
  59. }
  60. var url = ret.data.url || '';
  61. var params = $(that.element).data("params") || {};
  62. var category = typeof params.category !== 'undefined' ? params.category : ($(that.element).data("category") || '');
  63. category = typeof category === 'function' ? category.call(that, file) : category;
  64. Fast.api.ajax({
  65. url: "/addons/qiniu/index/notify",
  66. data: {name: file.name, url: ret.data.url, hash: ret.data.hash, size: file.size, width: file.width || 0, height: file.height || 0, type: file.type, category: category, qiniutoken: Config.upload.multipart.qiniutoken}
  67. }, function () {
  68. return false;
  69. }, function () {
  70. return false;
  71. });
  72. } else {
  73. console.error(ret);
  74. }
  75. } else {
  76. console.error(file.xhr);
  77. }
  78. } catch (e) {
  79. console.error(e);
  80. }
  81. _success.call(this, file, ret);
  82. });
  83. this.on("addedfile", function (file) {
  84. var that = this;
  85. setTimeout(function () {
  86. if (file.status === 'error') {
  87. return;
  88. }
  89. var md5 = ''; //七牛云无需本地获取文件MD5
  90. var chunk = that.options.chunking && file.size > that.options.chunkSize ? 1 : 0;
  91. var params = $(that.element).data("params") || {};
  92. var category = typeof params.category !== 'undefined' ? params.category : ($(that.element).data("category") || '');
  93. category = typeof category === 'function' ? category.call(that, file) : category;
  94. Fast.api.ajax({
  95. url: "/addons/qiniu/index/params",
  96. data: {method: 'POST', category: category, md5: md5, name: file.name, type: file.type, size: file.size, chunk: chunk, chunksize: that.options.chunkSize, qiniutoken: Config.upload.multipart.qiniutoken},
  97. }, function (data) {
  98. file.qiniutoken = data.qiniutoken;
  99. file.params = data;
  100. file.category = category;
  101. if (file.status != 'error') {
  102. //开始上传
  103. that.enqueueFile(file);
  104. } else {
  105. that.removeFile(file);
  106. }
  107. return false;
  108. }, function () {
  109. that.removeFile(file);
  110. });
  111. }, 0);
  112. });
  113. //如果是直传模式
  114. if (Config.upload.uploadmode === 'client') {
  115. var _url = this.options.url;
  116. //分片上传时URL链接不同
  117. this.options.url = function (files) {
  118. this.options.headers = {"Authorization": "UpToken " + Config.upload.multipart.qiniutoken};
  119. if (files[0].upload.chunked) {
  120. var chunk = null;
  121. files[0].upload.chunks.forEach(function (item) {
  122. if (item.status === 'uploading') {
  123. chunk = item;
  124. }
  125. });
  126. if (!chunk) {
  127. return Config.upload.uploadurl + '/mkfile/' + files[0].size;
  128. } else {
  129. return Config.upload.uploadurl + '/mkblk/' + chunk.dataBlock.data.size;
  130. }
  131. }
  132. return _url;
  133. };
  134. this.options.params = function (files, xhr, chunk) {
  135. var params = Config.upload.multipart;
  136. if (chunk) {
  137. return $.extend({}, params, {
  138. filesize: chunk.file.size,
  139. filename: chunk.file.name,
  140. chunkid: chunk.file.upload.uuid,
  141. chunkindex: chunk.index,
  142. chunkcount: chunk.file.upload.totalChunkCount,
  143. chunkfilesize: chunk.dataBlock.data.size,
  144. width: chunk.file.width || 0,
  145. height: chunk.file.height || 0,
  146. type: chunk.file.type,
  147. });
  148. } else {
  149. var retParams = $.extend({}, params, files[0].params || {});
  150. //七牛云直传使用的是token参数
  151. retParams.token = retParams.qiniutoken;
  152. delete retParams.qiniutoken;
  153. return retParams;
  154. }
  155. };
  156. //分片上传时需要变更提交的内容
  157. this.on("sending", function (file, xhr, formData) {
  158. if (file.upload.chunked) {
  159. var _send = xhr.send;
  160. xhr.send = function () {
  161. var chunk = null;
  162. file.upload.chunks.forEach(function (item) {
  163. if (item.status == 'uploading') {
  164. chunk = item;
  165. }
  166. });
  167. if (chunk) {
  168. _send.call(xhr, chunk.dataBlock.data);
  169. }
  170. };
  171. }
  172. });
  173. }
  174. };
  175. });