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 65 posts 307 karma points c-trib
    1 week ago
    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 585 karma points
    1 week ago
    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 65 posts 307 karma points c-trib
    5 days ago
    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 5730 posts 21691 karma points MVP 3x admin c-trib
    5 days ago
    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 65 posts 307 karma points c-trib
    5 days ago
    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