arkiwi.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*------------------------------------------------------------------------------------------------------------------------
  2. @package: Arkiwi Upload
  3. @author: cek
  4. @www: arkiwi.org
  5. @copyright: COPYRIGHT 25 cek
  6. @license: MIT
  7. =============================================================================
  8. Filename: arkiwi.js
  9. =============================================================================
  10. /*------------------------------------------------------------------------------------------------------------------------*/
  11. var ARKIWI = ARKIWI || {};
  12. ARKIWI.Uploader = function (uploaderUrl, defaultParameters, formName, sessionUploadProgressName) {
  13. this.uploaderUrl = uploaderUrl;
  14. this.defaultParameters = defaultParameters || '';
  15. this.formName = formName || 'arkiwiUploadForm';
  16. this.sessionUploadProgressName = sessionUploadProgressName || 'PHP_SESSION_UPLOAD_PROGRESS';
  17. };
  18. ARKIWI.Uploader.constructor = ARKIWI.Uploader;
  19. /* Invia un file all'uploader. */
  20. //ARKIWI.Uploader.prototype.upload = function (form, destinationFolderBase64, callbacks) {
  21. // form = $(form);
  22. //
  23. // form.attr('method', 'POST');
  24. // form.attr('action', this.uploaderUrl + '/upload/' + this.sessionId + '/' + destinationFolderBase64 + '/?' + this.defaultParameters);
  25. // form.attr('id', this.formName);
  26. // form.attr('enctype', 'multipart/form-data');
  27. // form.attr('target', 'arkiwi_hidden_iframe');
  28. //
  29. // form.append('<input type="hidden" value="' + this.formName + '" name="' + this.sessionUploadProgressName + '" />');
  30. //
  31. // $(':file', form).attr('name', this.sessionId);
  32. //
  33. // form.ajaxForm({
  34. // beforeSend: function () {
  35. // callbacks.beforeSend();
  36. // },
  37. // uploadProgress: function (event, position, total, percentComplete) {
  38. // callbacks.uploadProgress(event, position, total, percentComplete);
  39. // },
  40. // complete: function (xhr) {
  41. // callbacks.complete(xhr);
  42. // },
  43. // error: function (xhr, status, error) {
  44. // throw 'Arkiwi.upload(): error ' + error;
  45. // }
  46. // });
  47. //
  48. // form.submit();
  49. //};
  50. ARKIWI.Uploader.prototype.upload = function (file, destinationFolderBase64, callbacks) {
  51. var xhr = new XMLHttpRequest();
  52. xhr.withCredentials = true;
  53. var self = this;
  54. xhr.upload.addEventListener("progress", function(e) {}, false);
  55. xhr.upload.addEventListener("load", function(e){}, false);
  56. xhr.open("POST", this.uploaderUrl + '/upload/' + destinationFolderBase64 + '/?' + this.defaultParameters, true);
  57. var fileData = new FormData();
  58. fileData.append("arkiwiuploadedfile", file);
  59. // These extra params aren't necessary but show that you can include other data.
  60. fileData.append("arkiwiuploadedfilename", file.name);
  61. xhr.send(fileData);
  62. };
  63. /* Aggiunge o sovrascrive i metadati di un file. */
  64. ARKIWI.Uploader.prototype.metadata = function (jsonKVString, callback) {
  65. $.ajax({
  66. url: this.uploaderUrl + '/metadata/' + this.sessionId + '/?' + this.defaultParameters,
  67. type: 'POST',
  68. async: true,
  69. cache: false,
  70. context: this,
  71. data: jsonKVString,
  72. dataType: 'json',
  73. error: function (xhr, status, error) {
  74. throw 'Arkiwi.metadata(): status ' + status + ' error ' + error;
  75. },
  76. success: function (result, status, xhr) {
  77. if (callback != undefined)
  78. callback(result);
  79. },
  80. complete: function (xhr, status) {}
  81. });
  82. };
  83. /* Cancella un metadato da un file */
  84. ARKIWI.Uploader.prototype.removeMetadata = function (jsonKVString, callback) {
  85. $.ajax({
  86. url: this.uploaderUrl + '/removemetadata/' + this.sessionId + '/?' + this.defaultParameters,
  87. type: 'POST',
  88. async: true,
  89. cache: false,
  90. context: this,
  91. data: jsonKVString,
  92. dataType: 'json',
  93. error: function (xhr, status, error) {
  94. throw 'Arkiwi.removeMetadata(): status ' + status + ' error ' + error;
  95. },
  96. success: function (result, status, xhr) {
  97. if (callback != undefined)
  98. callback(result);
  99. },
  100. complete: function (xhr, status) {}
  101. });
  102. };
  103. /* Chiude la sessione con l'asset store */
  104. ARKIWI.Uploader.prototype.close = function (callback) {
  105. $.ajax({
  106. url: this.uploaderUrl + '/close/' + this.sessionId + '/?' + this.defaultParameters,
  107. type: 'GET',
  108. async: true,
  109. cache: false,
  110. context: this,
  111. data: '',
  112. dataType: 'json',
  113. error: function (xhr, status, error) {
  114. throw 'Arkiwi.close(): status ' + status + ' error ' + error;
  115. },
  116. success: function (result, status, xhr) {
  117. if (callback != undefined)
  118. callback(result);
  119. },
  120. complete: function (xhr, status) {}
  121. });
  122. };