diff --git a/app/client/cypress/integration/Smoke_TestSuite/ClientSideTests/DisplayWidgets/Image_spec.js b/app/client/cypress/integration/Smoke_TestSuite/ClientSideTests/DisplayWidgets/Image_spec.js index f3d2afe846..4840ad5584 100644 --- a/app/client/cypress/integration/Smoke_TestSuite/ClientSideTests/DisplayWidgets/Image_spec.js +++ b/app/client/cypress/integration/Smoke_TestSuite/ClientSideTests/DisplayWidgets/Image_spec.js @@ -64,10 +64,21 @@ describe("Image Widget Functionality", function() { cy.togglebar(commonlocators.visibleCheckbox); cy.PublishtheApp(); cy.get(publish.imageWidget).should("be.visible"); + cy.get(publish.backToEditor).click(); + }); + + it("Image Widget Functionality To check download option and validate image link", function() { + cy.openPropertyPane("imagewidget"); + cy.togglebar(".t--property-control-enabledownload input[type='checkbox']"); + cy.get(publish.imageWidget).trigger("mouseover"); + cy.get(`${publish.imageWidget} a[data-cy=t--image-download]`).should( + "have.attr", + "href", + this.data.NewImage, + ); }); it("In case of an image loading error, show off the error message", () => { - cy.get(publish.backToEditor).click(); cy.openPropertyPane("imagewidget"); // Invalid image url const invalidImageUrl = "https://www.example.com/does-not-exist.jpg"; diff --git a/app/client/src/widgets/ImageWidget/component/index.tsx b/app/client/src/widgets/ImageWidget/component/index.tsx index 5b9c53235a..75bf80cb6d 100644 --- a/app/client/src/widgets/ImageWidget/component/index.tsx +++ b/app/client/src/widgets/ImageWidget/component/index.tsx @@ -57,7 +57,7 @@ const ControlBtnWrapper = styled.div` background: white; `; -const ControlBtn = styled.div` +const ControlBtn = styled.a` height: 25px; width: 45px; color: white; @@ -242,6 +242,7 @@ class ImageComponent extends React.Component< } = this.props; const { showImageControl } = this.state; const showDownloadBtn = enableDownload && (!!imageUrl || !!defaultImageUrl); + const hrefUrl = imageUrl || defaultImageUrl; if (showImageControl && (enableRotation || showDownloadBtn)) { return ( @@ -290,7 +291,12 @@ class ImageComponent extends React.Component< )} {showDownloadBtn && ( - +
{ - const { defaultImageUrl, imageUrl, widgetId } = this.props; - const fileName = `${widgetId}-download`; - const downloadUrl = imageUrl || defaultImageUrl; - - const xhr = new XMLHttpRequest(); - xhr.open("GET", downloadUrl, true); - xhr.responseType = "blob"; - - xhr.onload = function() { - const urlCreator = window.URL || window.webkitURL; - const imageUrlObj = urlCreator.createObjectURL(this.response); - const tag = document.createElement("a"); - tag.href = imageUrlObj; - tag.download = fileName; - document.body.appendChild(tag); - tag.click(); - document.body.removeChild(tag); - window.URL.revokeObjectURL(imageUrlObj); - }; - // if download fails open image in new tab - xhr.onerror = function() { - const tag = document.createElement("a"); - tag.href = downloadUrl; - tag.target = "_blank"; - document.body.appendChild(tag); - tag.click(); - document.body.removeChild(tag); - }; - xhr.send(); - - if (!!e) { - e.preventDefault(); - e.stopPropagation(); - } - }; - onMouseEnter = () => { const { defaultImageUrl, imageUrl } = this.props; if (defaultImageUrl || imageUrl) {