Skip to main content
Level 2
December 16, 2022
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.

Can any one please provide inputs on the same?

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
SantoshSai
SantoshSaiAccepted solution
Community Advisor
December 16, 2022

@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();
            }
Santosh Sai

3 replies

SantoshSai
Community Advisor
Community Advisor
December 16, 2022

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-inline-in-the-author-view-as-published/m-p/563535#M140375

Hope that helps!

Regards,

Santosh

Translate
Santosh Sai
Level 2
December 16, 2022

Hi Santhosh,

currently working on local only.

I have uploaded my .txt file under dam folder.

is there any other way to do it?

Translate
SantoshSai
Community Advisor
SantoshSaiCommunity AdvisorAccepted solution
Community Advisor
December 16, 2022

@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();
            }
Translate
Santosh Sai