arkiwi.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 (file, destinationFolderBase64, callbacks) {
  21. var xhr = new XMLHttpRequest();
  22. xhr.withCredentials = true;
  23. var self = this;
  24. if (callbacks) {
  25. xhr.upload.addEventListener("progress", function(e) {
  26. callbacks.progress(e);
  27. }, false);
  28. //we need to add the event listener to xhr, not to xhr.upload
  29. xhr.addEventListener("load", function(e){
  30. callbacks.load(e);
  31. }, false);
  32. xhr.upload.addEventListener("error", function(e){
  33. callbacks.error(e);
  34. }, false);
  35. }
  36. xhr.open("POST", this.uploaderUrl + '/upload/' + destinationFolderBase64 + '/?' + this.defaultParameters, true);
  37. var fileData = new FormData();
  38. fileData.append("arkiwiuploadedfile", file);
  39. // These extra params aren't necessary but show that you can include other data.
  40. fileData.append("arkiwiuploadedfilename", file.name);
  41. xhr.send(fileData);
  42. };
  43. /* Aggiunge o sovrascrive i metadati di un file. */
  44. ARKIWI.Uploader.prototype.listMetadata = function (item64, callback) {
  45. $.ajax({
  46. url: this.uploaderUrl + '/listmetadata/' + item64 + '/?' + this.defaultParameters,
  47. type: 'GET',
  48. async: true,
  49. cache: false,
  50. context: this,
  51. data: '',
  52. dataType: 'json',
  53. error: function (xhr, status, error) {
  54. if (status != 'parsererror')
  55. throw 'Arkiwi.listMetadata(): status ' + status + ' error ' + error;
  56. else {
  57. if (callback)
  58. callback(JSON.parse("[]"));
  59. }
  60. },
  61. success: function (result, status, xhr) {
  62. if (callback != undefined)
  63. callback(result);
  64. },
  65. complete: function (xhr, status) {}
  66. });
  67. };
  68. /* Aggiunge o sovrascrive i metadati di un file. */
  69. ARKIWI.Uploader.prototype.modifyMetadata = function (item64, jsonKVString, callback) {
  70. $.ajax({
  71. url: this.uploaderUrl + '/modifymetadata/' + item64 + '/?' + this.defaultParameters,
  72. type: 'POST',
  73. async: true,
  74. cache: false,
  75. context: this,
  76. data: jsonKVString,
  77. dataType: 'json',
  78. contentType: "application/json",
  79. error: function (xhr, status, error) {
  80. throw 'Arkiwi.modifyMetadata(): status ' + status + ' error ' + error;
  81. },
  82. success: function (result, status, xhr) {
  83. if (callback)
  84. callback(result);
  85. },
  86. complete: function (xhr, status) {}
  87. });
  88. };
  89. /* Aggiunge o sovrascrive i metadati di un file. */
  90. ARKIWI.Uploader.prototype.createDirectory = function (item64, newDirectory, callback) {
  91. $.ajax({
  92. url: this.uploaderUrl + '/createdirectory/' + item64 + '/?' + this.defaultParameters,
  93. type: 'POST',
  94. async: true,
  95. cache: false,
  96. context: this,
  97. data: JSON.stringify({directory : newDirectory}),
  98. dataType: 'text',
  99. contentType: "html/text",
  100. error: function (xhr, status, error) {
  101. throw 'Arkiwi.createDirectory(): status ' + status + ' error ' + error;
  102. },
  103. success: function (result, status, xhr) {
  104. if (callback != undefined)
  105. callback(result);
  106. },
  107. complete: function (xhr, status) {}
  108. });
  109. };