Upload SVG image such that svg tag is retained | Community
Skip to main content
Level 3
October 14, 2021
Solved

Upload SVG image such that svg tag is retained

  • October 14, 2021
  • 1 reply
  • 1795 views

Hi All,

 

I have a requirement to upload an SVG image such that it retains its tag and values in jsp.

For example, if uploaded images has dam path "/content/image.svg", in html element, it should look like 

"<svg id="CVAD" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1620.33 891">
<g id="Section_5" data-name="Section 5"><path d="M1409.64,862.85c1.18.06,2.35.4,3.54.45,1.4.06,2.84.06,4.23,0s2.81-.37,4.16-.35a14.68,14.68,0,0,1,4.09.75,11.92,11.92,0,0,0,5-1.23" transform="translate(-149.94 -94.5)"/>18.46,3.77,3.77,0,0,0-2.58,2.67A8.08,8.08,0,0,0,761.58,223.06Z" transform="translate(-149.94 -94.5)"/></g>
</svg>"

 

Any pointer on this.

 

TIA

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by Anudeep_Garnepudi

@annkitaaggarwal 

For that you need to write a code to extract the content of the svg file and display from HTL/JSP.

In your Sling Model or Scriptlet

InputStream stream = resolver.getResource("/content/dam/../image.svg/jcr:content/renditions/original/jcr:content").adaptTo(javax.jcr.Node.class).getProperty("jcr:data").getBinary().getStream();
String markup = new String(stream.readAllBytes(), StandardCharsets.UTF_8);
stream.close();

 If you are using HTL, return the string and use below line in HTML

${myModel.svgMarkUp @ context='unsafe'}

If you are using JSP, print the string value directly.

1 reply

Anudeep_Garnepudi
Community Advisor
Anudeep_GarnepudiCommunity AdvisorAccepted solution
Community Advisor
October 16, 2021

@annkitaaggarwal 

For that you need to write a code to extract the content of the svg file and display from HTL/JSP.

In your Sling Model or Scriptlet

InputStream stream = resolver.getResource("/content/dam/../image.svg/jcr:content/renditions/original/jcr:content").adaptTo(javax.jcr.Node.class).getProperty("jcr:data").getBinary().getStream();
String markup = new String(stream.readAllBytes(), StandardCharsets.UTF_8);
stream.close();

 If you are using HTL, return the string and use below line in HTML

${myModel.svgMarkUp @ context='unsafe'}

If you are using JSP, print the string value directly.

AG
Level 3
October 18, 2021

can you tell me a way to do it in java 8

Anudeep_Garnepudi
Community Advisor
Community Advisor
October 18, 2021

Java 8 or 11, same approach will work.

AG