/*------------------------------------------------------------------------------------------------------------------------ @package: Arkiwi Upload @author: cek @www: arkiwi.org @copyright: COPYRIGHT 25 cek @license: MIT ============================================================================= Filename: arkiwi.js ============================================================================= /*------------------------------------------------------------------------------------------------------------------------*/ var ARKIWI = ARKIWI || {}; ARKIWI.Uploader = function (uploaderUrl, defaultParameters, formName, sessionUploadProgressName) { this.uploaderUrl = uploaderUrl; this.defaultParameters = defaultParameters || ''; this.formName = formName || 'arkiwiUploadForm'; this.sessionUploadProgressName = sessionUploadProgressName || 'PHP_SESSION_UPLOAD_PROGRESS'; }; ARKIWI.Uploader.constructor = ARKIWI.Uploader; /* Invia un file all'uploader. */ ARKIWI.Uploader.prototype.upload = function (file, destinationFolderBase64, callbacks) { var xhr = new XMLHttpRequest(); xhr.withCredentials = true; var self = this; if (callbacks) { xhr.upload.addEventListener("progress", function(e) { callbacks.progress(e); }, false); //we need to add the event listener to xhr, not to xhr.upload xhr.addEventListener("load", function(e){ callbacks.load(e); }, false); xhr.upload.addEventListener("error", function(e){ callbacks.error(e); }, false); } xhr.open("POST", this.uploaderUrl + '/upload/' + destinationFolderBase64 + '/?' + this.defaultParameters, true); var fileData = new FormData(); fileData.append("arkiwiuploadedfile", file); // These extra params aren't necessary but show that you can include other data. fileData.append("arkiwiuploadedfilename", file.name); xhr.send(fileData); }; /* Aggiunge o sovrascrive i metadati di un file. */ ARKIWI.Uploader.prototype.listMetadata = function (item64, callback) { $.ajax({ url: this.uploaderUrl + '/listmetadata/' + item64 + '/?' + this.defaultParameters, type: 'GET', async: true, cache: false, context: this, data: '', dataType: 'json', error: function (xhr, status, error) { if (status != 'parsererror') throw 'Arkiwi.listMetadata(): status ' + status + ' error ' + error; else { if (callback) callback(JSON.parse("[]")); } }, success: function (result, status, xhr) { if (callback != undefined) callback(result); }, complete: function (xhr, status) {} }); }; /* Aggiunge o sovrascrive i metadati di un file. */ ARKIWI.Uploader.prototype.modifyMetadata = function (item64, jsonKVString, callback) { $.ajax({ url: this.uploaderUrl + '/modifymetadata/' + item64 + '/?' + this.defaultParameters, type: 'POST', async: true, cache: false, context: this, data: jsonKVString, dataType: 'json', contentType: "application/json", error: function (xhr, status, error) { throw 'Arkiwi.modifyMetadata(): status ' + status + ' error ' + error; }, success: function (result, status, xhr) { if (callback) callback(result); }, complete: function (xhr, status) {} }); }; /* Aggiunge o sovrascrive i metadati di un file. */ ARKIWI.Uploader.prototype.createDirectory = function (item64, newDirectory, callback) { $.ajax({ url: this.uploaderUrl + '/createdirectory/' + item64 + '/?' + this.defaultParameters, type: 'POST', async: true, cache: false, context: this, data: JSON.stringify({directory : newDirectory}), dataType: 'text', contentType: "html/text", error: function (xhr, status, error) { throw 'Arkiwi.createDirectory(): status ' + status + ' error ' + error; }, success: function (result, status, xhr) { if (callback != undefined) callback(result); }, complete: function (xhr, status) {} }); };