Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Mikael Axel Kleinwort 140 posts 484 karma points c-trib
    May 15, 2020 @ 19:14
    Mikael Axel Kleinwort
    0

    Read svg from media library

    In Umbraco 8.5, I want to include the content of image media items of type SVG directly in html markup inside of a partial view with @Html.Raw(...)

    What is the best way to retrieve the content of the media items so the data is properly cached?

    This is what works for me right now:

        @Html.Raw(System.IO.File.ReadAllText(Server.MapPath(img.Url)));
    

    but this seems to be not cache-friendly.

  • Dmitriy 168 posts 588 karma points
    May 16, 2020 @ 18:04
    Dmitriy
    0

    Hi

    Do you want just render SVG to page's DOM?

    You can avoid manual file IO operation and render the only URL to DOM and then use a JavaScript library (like svg4everybody) that handles the URL and append file content into DOM.

  • Mikael Axel Kleinwort 140 posts 484 karma points c-trib
    May 21, 2020 @ 15:18
    Mikael Axel Kleinwort
    0

    Dmitriy,

    thank you for taking the time to answer. I want to be able to modify part of the svg tags prior to rendering, this is why I am looking for a server-side, cache-friendly solution to this.

  • Alex Skrypnyk 6131 posts 23950 karma points MVP 7x admin c-trib
    May 21, 2020 @ 16:12
    Alex Skrypnyk
    0

    Hi Mikael

    The problem with code you use is IO library, the slowest part is reading the file from the disk.

    I would recommend you just add caching to the Partial level for example, it's super easy to do with Umbraco helper Html.CachedPartial

    Thanks,

    Alex

  • Mikael Axel Kleinwort 140 posts 484 karma points c-trib
    May 21, 2020 @ 16:23
    Mikael Axel Kleinwort
    0

    Alex.

    great to hear from you. Thank you for pointing out the obvious to me! :-)

    I was suspecting that there might be a better "Umbraco" way to get to the svg content apart from using the IO library, but keeping this code and caching the partial is a great idea. Thanks again.

Please Sign in or register to post replies

Write your reply to:

Draft