Expand my Community achievements bar.

Enhance your AEM Assets & Boost Your Development: [AEM Gems | June 19, 2024] Improving the Developer Experience with New APIs and Events
SOLVED

Create a text file under /content/dam/abc called dam.txt, Usually the txt file gets downloaded on author instance, Think of a way to display its content on the browser instead of download.

Avatar

Level 2

Can any one please provide inputs on the same?

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

@jhansi_123 I believe you have created custom component to render txt content - In that case please follow below steps.

  • Create Sling Model
  • Create OSGi Service
  • Refer/ Call to that OSGi service through Sling Model 
  • Render that content in HTL with the help of Sling Model

Please refer below sample code which you may need to read txt from JCR. 

File should have an nt:file node, which has a jcr:content node with jcr:data property.
You can read the xml from jcr:data i.e: jcrContent.getProperty("jcr:data").getBinary().getStream();/

InputStream is = jcrContent.getProperty("jcr:data").getBinary()
                    .getStream();

            BufferedInputStream bis = new BufferedInputStream(is);
            ByteArrayOutputStream buf = new ByteArrayOutputStream();
            int resultNumber = bis.read();
            while (resultNumber != -1) {
                byte b = (byte) resultNumber;
                buf.write(b);
                resultNumber = bis.read();
            }

View solution in original post

3 Replies

Avatar

Community Advisor

Hi @jhansi_123 ,

You can achieve this by "Apache Sling Content Disposition Filter" which controls Content Disposition for different mime types based on the path.

Please refer to the similar thread below which I recently posted the answer for pdf and similar approach for txt. 

https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/how-can-i-have-pdf-open-in...

Hope that helps!

Regards,

Santosh

Avatar

Level 2

Hi Santhosh,

currently working on local only.

I have uploaded my .txt file under dam folder.

is there any other way to do it?

Avatar

Correct answer by
Community Advisor

@jhansi_123 I believe you have created custom component to render txt content - In that case please follow below steps.

  • Create Sling Model
  • Create OSGi Service
  • Refer/ Call to that OSGi service through Sling Model 
  • Render that content in HTL with the help of Sling Model

Please refer below sample code which you may need to read txt from JCR. 

File should have an nt:file node, which has a jcr:content node with jcr:data property.
You can read the xml from jcr:data i.e: jcrContent.getProperty("jcr:data").getBinary().getStream();/

InputStream is = jcrContent.getProperty("jcr:data").getBinary()
                    .getStream();

            BufferedInputStream bis = new BufferedInputStream(is);
            ByteArrayOutputStream buf = new ByteArrayOutputStream();
            int resultNumber = bis.read();
            while (resultNumber != -1) {
                byte b = (byte) resultNumber;
                buf.write(b);
                resultNumber = bis.read();
            }