bootstrap.js 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. //如果开启了alioss客户端上传模式
  2. if (typeof Config.upload.storage !== 'undefined' && Config.upload.storage === 'alioss') {
  3. require(['upload', '../addons/alioss/js/spark'], function (Upload, SparkMD5) {
  4. var _onFileAdded = Upload.events.onFileAdded;
  5. var _onUploadResponse = Upload.events.onUploadResponse;
  6. var _process = function (up, file) {
  7. (function (up, file) {
  8. var blob = file.getNative();
  9. var loadedBytes = file.loaded;
  10. var chunkSize = 2097152;
  11. var chunkBlob = blob.slice(loadedBytes, loadedBytes + chunkSize);
  12. var reader = new FileReader();
  13. reader.addEventListener('loadend', function (e) {
  14. var spark = new SparkMD5.ArrayBuffer();
  15. spark.append(e.target.result);
  16. var md5 = spark.end();
  17. Fast.api.ajax({
  18. url: "/addons/alioss/index/params",
  19. data: {method: 'POST', md5: md5, name: file.name, type: file.type, size: file.size},
  20. }, function (data) {
  21. file.md5 = md5;
  22. file.status = 1;
  23. file.key = data.key;
  24. file.OSSAccessKeyId = data.id;
  25. file.policy = data.policy;
  26. file.signature = data.signature;
  27. up.start();
  28. return false;
  29. });
  30. return;
  31. });
  32. reader.readAsArrayBuffer(chunkBlob);
  33. })(up, file);
  34. };
  35. Upload.events.onFileAdded = function (up, files) {
  36. return _onFileAdded.call(this, up, files);
  37. };
  38. Upload.events.onBeforeUpload = function (up, file) {
  39. if (typeof file.md5 === 'undefined') {
  40. up.stop();
  41. _process(up, file);
  42. } else {
  43. up.settings.headers = up.settings.headers || {};
  44. up.settings.multipart_params.key = file.key;
  45. up.settings.multipart_params.OSSAccessKeyId = file.OSSAccessKeyId;
  46. up.settings.multipart_params.success_action_status = 200;
  47. if (typeof file.callback !== 'undefined') {
  48. up.settings.multipart_params.callback = file.callback;
  49. }
  50. up.settings.multipart_params.policy = file.policy;
  51. up.settings.multipart_params.signature = file.signature;
  52. //up.settings.send_file_name = false;
  53. }
  54. };
  55. Upload.events.onUploadResponse = function (response, info, up, file) {
  56. try {
  57. var ret = {};
  58. if (info.status === 200) {
  59. var url = '/' + file.key;
  60. Fast.api.ajax({
  61. url: "/addons/alioss/index/notify",
  62. data: {method: 'POST', name: file.name, url: url, md5: file.md5, size: file.size, type: file.type, policy: file.policy, signature: file.signature}
  63. }, function () {
  64. return false;
  65. });
  66. ret.code = 1;
  67. ret.data = {
  68. url: url
  69. };
  70. } else {
  71. ret.code = 0;
  72. ret.msg = info.response;
  73. }
  74. return _onUploadResponse.call(this, JSON.stringify(ret));
  75. } catch (e) {
  76. }
  77. return _onUploadResponse.call(this, response);
  78. };
  79. });
  80. }