From 611e5114bc15a53a0184dda54e7a10c7c65f00ec Mon Sep 17 00:00:00 2001 From: Jorge Miguel Lobo Escalona <47326048+jloboescalona2@users.noreply.github.com> Date: Tue, 16 Mar 2021 18:15:26 +0100 Subject: [PATCH] F #5056: fix upload image (#984) Co-authored-by: Jorge Lobo --- .../images-tab/form-panels/create-common.js | 69 ++++++++++--------- 1 file changed, 37 insertions(+), 32 deletions(-) diff --git a/src/sunstone/public/app/tabs/images-tab/form-panels/create-common.js b/src/sunstone/public/app/tabs/images-tab/form-panels/create-common.js index 8bbbf7bef8..aa0294e833 100644 --- a/src/sunstone/public/app/tabs/images-tab/form-panels/create-common.js +++ b/src/sunstone/public/app/tabs/images-tab/form-panels/create-common.js @@ -201,8 +201,12 @@ define(function(require) { function _setup(context) { + var that = this; $.each(this.wizardTabs, function(index, wizardTab) { wizardTab.setup($("#" + wizardTab.wizardTabId, context)); + if(wizardTab.title === "Image"){ + that.uploader = wizardTab.uploader; + } }); Foundation.reflow(context, "tabs"); Foundation.reflow(context, "tooltip"); @@ -307,39 +311,40 @@ define(function(require) { //this is an image upload we trigger FileUploader //to start the upload if (upload) { - if (that.uploader.files.length == 0) { - Sunstone.hideFormPanelLoading(that.tabId); - Notifier.notifyError(Locale.tr("Please select a file to upload")); - return false; - } - - Sunstone.resetFormPanel(that.tabId, that.formPanelId); - Sunstone.hideFormPanel(that.tabId); - - that.uploader.on("fileSuccess", function(file) { - $("div[id=\"" + file.fileName + "-info\"]").text(Locale.tr("Registering in OpenNebula")); - $.ajax({ - url: "upload", - type: "POST", - data: { - csrftoken: csrftoken, - img : JSON.stringify(img_obj), - file: file.fileName, - tempfile: file.uniqueIdentifier - }, - success: function() { - Notifier.notifyMessage("Image uploaded correctly"); - $("div[id=\"" + file.fileName + "progressBar\"]").remove(); - Sunstone.runAction(that.resource+".refresh"); - }, - error: function(response) { - Notifier.onError({}, OpenNebulaError(response)); - $("div[id=\"" + file.fileName + "progressBar\"]").remove(); - } + if(that.uploader){ + if (that.uploader.files && that.uploader.files.length == 0) { + Sunstone.hideFormPanelLoading(that.tabId); + Notifier.notifyError(Locale.tr("Please select a file to upload")); + return false; + } + Sunstone.resetFormPanel(that.tabId, that.formPanelId); + Sunstone.hideFormPanel(that.tabId); + that.uploader.on("fileSuccess", function(file) { + $("div[id=\"" + file.fileName + "-info\"]").text(Locale.tr("Registering in OpenNebula")); + $.ajax({ + url: "upload", + type: "POST", + data: { + csrftoken: csrftoken, + img : JSON.stringify(img_obj), + file: file.fileName, + tempfile: file.uniqueIdentifier + }, + success: function() { + Notifier.notifyMessage("Image uploaded correctly"); + $("div[id=\"" + file.fileName + "progressBar\"]").remove(); + Sunstone.runAction(that.resource+".refresh"); + }, + error: function(response) { + Notifier.onError({}, OpenNebulaError(response)); + $("div[id=\"" + file.fileName + "progressBar\"]").remove(); + } + }); }); - }); - - that.uploader.upload(); + that.uploader.upload(); + }else{ + Notifier.notifyError(Locale.tr("you don't have a valid uploader")); + } } else { Sunstone.runAction(that.resource+".create", img_obj); }