bootstrap.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. //修改上传的接口调用
  2. require(['upload'], function (Upload) {
  3. //初始化中完成判断
  4. Upload.events.onInit = function () {
  5. //如果上传接口不是七牛云,则不处理
  6. if (this.options.url !== Config.upload.uploadurl) {
  7. return;
  8. }
  9. var _success = this.options.success;
  10. $.extend(this.options, {
  11. chunkSuccess: function (chunk, file, response) {
  12. this.contexts = this.contexts ? this.contexts : [];
  13. this.contexts.push(typeof response.ctx !== 'undefined' ? response.ctx : response.data.ctx);
  14. },
  15. chunksUploaded: function (file, done) {
  16. var that = this;
  17. Fast.api.ajax({
  18. url: "/addons/qiniu/index/upload",
  19. data: {
  20. action: 'merge',
  21. filesize: file.size,
  22. filename: file.name,
  23. chunkid: file.upload.uuid,
  24. chunkcount: file.upload.totalChunkCount,
  25. width: file.width || 0,
  26. height: file.height || 0,
  27. type: file.type,
  28. qiniutoken: Config.upload.multipart.qiniutoken,
  29. contexts: this.contexts
  30. },
  31. }, function (data, ret) {
  32. done(JSON.stringify(ret));
  33. return false;
  34. }, function (data, ret) {
  35. file.accepted = false;
  36. that._errorProcessing([file], ret.msg);
  37. return false;
  38. });
  39. },
  40. });
  41. //先移除已有的事件
  42. this.off("success", _success).on("success", function (file, response) {
  43. var ret = {code: 0, msg: response};
  44. try {
  45. ret = typeof response === 'string' ? JSON.parse(response) : response;
  46. if (file.xhr.status === 200) {
  47. if (typeof ret.key !== 'undefined') {
  48. ret = {code: 1, msg: "", data: {url: '/' + ret.key, hash: ret.hash}};
  49. }
  50. Fast.api.ajax({
  51. url: "/addons/qiniu/index/notify",
  52. 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, qiniutoken: Config.upload.multipart.qiniutoken}
  53. }, function () {
  54. return false;
  55. }, function () {
  56. return false;
  57. });
  58. }
  59. } catch (e) {
  60. console.error(e);
  61. }
  62. _success.call(this, file, ret);
  63. });
  64. //如果是直传模式
  65. if (Config.upload.uploadmode === 'client') {
  66. var _url = this.options.url;
  67. //分片上传时URL链接不同
  68. this.options.url = function (files) {
  69. this.options.headers = {"Authorization": "UpToken " + Config.upload.multipart.qiniutoken};
  70. if (files[0].upload.chunked) {
  71. var chunk = null;
  72. files[0].upload.chunks.forEach(function (item) {
  73. if (item.status === 'uploading') {
  74. chunk = item;
  75. }
  76. });
  77. if (!chunk) {
  78. return Config.upload.uploadurl + '/mkfile/' + files[0].size;
  79. } else {
  80. return Config.upload.uploadurl + '/mkblk/' + chunk.dataBlock.data.size;
  81. }
  82. }
  83. return _url;
  84. };
  85. this.options.params = function (files, xhr, chunk) {
  86. var params = Config.upload.multipart;
  87. if (chunk) {
  88. return $.extend({}, params, {
  89. filesize: chunk.file.size,
  90. filename: chunk.file.name,
  91. chunkid: chunk.file.upload.uuid,
  92. chunkindex: chunk.index,
  93. chunkcount: chunk.file.upload.totalChunkCount,
  94. chunkfilesize: chunk.dataBlock.data.size,
  95. width: chunk.file.width || 0,
  96. height: chunk.file.height || 0,
  97. type: chunk.file.type,
  98. });
  99. } else {
  100. var retParams = $.extend({}, params);
  101. //七牛云直传使用的是token参数
  102. retParams.token = retParams.qiniutoken;
  103. delete retParams.qiniutoken;
  104. return retParams;
  105. }
  106. };
  107. //分片上传时需要变更提交的内容
  108. this.on("sending", function (file, xhr, formData) {
  109. if (file.upload.chunked) {
  110. var _send = xhr.send;
  111. xhr.send = function () {
  112. var chunk = null;
  113. file.upload.chunks.forEach(function (item) {
  114. if (item.status == 'uploading') {
  115. chunk = item;
  116. }
  117. });
  118. if (chunk) {
  119. _send.call(xhr, chunk.dataBlock.data);
  120. }
  121. };
  122. }
  123. });
  124. }
  125. };
  126. });