How to fetch image from Sling Model | Community
Skip to main content
This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
ksh_ingole7
ksh_ingole7Accepted solution
New Participant
Aug 26, 2022 3:30 PM UTC

8 replies

HeenaMadan
New Participant
Aug 27, 2022 6:41 AM UTC
Shivam153
Level 3
Aug 26, 2022 6:57 PM UTC

Use the pathbrowser field in the dialog, you will get the path from here. After getting the path in Sling Model, use that object image src reference.

Please use below link for reference: 

https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/how-to-render-image-via-sightly/td-p/542959

Translate
ksh_ingole7
New Participant
Aug 26, 2022 12:54 PM UTC

Hi @sanjana12

 

In Sling Model kindly do the below,

1. Get The resource .

2. From the resource get the resource path and append to it the path of the Binary File and store it in the imagePath variable as below 

imagePath=resource.getPath()+"/photo/image";

3. Write a getter method to return the imagePath as below

@Override
    public String getImagePath() {
        return imagePath;
    }

4. In sightly call the model and then call the getter method to fetch the imagePath

<div data-sly-use.sampleModelObject="com.xyz.aem.tutorials.core.models.ComponentModel"></div>

<img src="${sampleModelObject.imagePath}" alt="User-Profile-Image">

 

Please refer to the below code :

https://github.com/aemgeeks1212/geekstutorials/blob/main/core/src/main/java/com/geeks/aem/tutorials/core/models/Impl/AuthorProfileImpl.java

 

If this doesn't help I would recommend you to go through the below Youtube Tutorial :

https://www.youtube.com/watch?v=BnU4AXWcKx4

Translate
Sanjana12Author
Level 6
Aug 26, 2022 1:39 PM UTC

What if I just want to drag and drop and not upload from local filesystem?

Translate
ksh_ingole7
ksh_ingole7Accepted solution
New Participant
Aug 26, 2022 3:30 PM UTC

If you want to drag and drop image already present in DAM, you can use below for Reference

Model - https://github.com/adobe/aem-core-wcm-components/blob/main/bundles/core/src/main/java/com/adobe/cq/wcm/core/components/models/Image.java

Sightly - https://github.com/adobe/aem-core-wcm-components/blob/main/content/src/content/jcr_root/apps/core/wcm/components/image/v1/image/image.html

 

In this case you would be returning the fileReference to sightly.

Translate
ksh_ingole7
New Participant
Aug 26, 2022 12:29 PM UTC

Hi @sanjana12 

 

In Sling model, you need to follow the below steps :

1. Get your resource in Sling Model.

2. Get the path of the resource using resource.getPath and append to it the path where the Binary file is stored and store the entire path in a variable say imagePath

imagePath=resource.getPath()+"/photo/image";

3. Now write a getter method for the same

@Override
    public String getImagePath() {
        return imagePath;
    }

4. Now in sightly call the Model and then call the getter method as below :

<div data-sly-use.sampleModel="com.xyz.aem.tutorials.core.models.ComponentModel"></div>

<img src="${sampleModel.imagePath}" class="image" alt="User-Profile-Image">

 

Refer to : https://github.com/aemgeeks1212/geekstutorials/blob/main/core/src/main/java/com/geeks/aem/tutorials/core/models/Impl/AuthorProfileImpl.java

 

If this doesn't help you I would recommend you to go through the below YouTube tutorial 

https://youtu.be/BnU4AXWcKx4

Translate
Sanjana12Author
Level 6
Aug 26, 2022 11:16 AM UTC

I need to fetch the image path and map as resources. How to do it? Please help. I'm new to this concept

Translate