1

Using 2sxc on DNN, I have a website that uses SVGs for icons in content types. The client wants to be able to upload the SVG icons to 2sxc via a Link field but then instead of rendering <img src="@Content.SVG" />, they want it to render the source code of the SVG (so we could manipulate the fill color via CSS). Is this even possible and how could it be done?

  • If it is possible, would there be a security risk to doing this? – Aaron - Wolf X Machina Nov 17 '20 at 19:43
  • 1
    Yes. SVG files can include scripts. So anything your page can do, the SVG can do. So you need to either (i) trust your users, or (ii) block files with potentially dangerous content (` – Paul LeBeau Nov 18 '20 at 10:45
  • I stuffed all Icon SVG data into strings; and then everything into **one** Custom Element file, which again outputs IMG or SVG, and has all styling options: https://IconMeister.github.io – Danny '365CSI' Engelman Nov 18 '20 at 15:49

3 Answers3

1

Some examples of how to do this can be found below

Using the fetch API

How to convert image (svg) to rendered svg in javascript?

Older methods such as XMLHttpRequest or jQuery

Include SVG files with HTML, and still be able to apply styles to them?

Using D3

(Embed and) Refer to an external SVG via D3 and/or javascript

Using a custom JS library

One example: SVGInjector

Paul LeBeau
  • 97,474
  • 9
  • 154
  • 181
1

Interestingly Dnn is doing this nowadays and you can look at the code here. If you ignore the caching, you might be able to do similar in a View. https://github.com/dnnsoftware/Dnn.Platform/blob/0d3b931d8483b01aaad0291a5fde2cbc0bac60ca/DNN%20Platform/Website/admin/Skins/Logo.ascx.cs#L123

And that is called from above, see ~line 71, so they are doing a real inject of the file contents to inline. Obviously caching file-access stuff should be a priority for caching if the website is high-traffic, but otherwise it is not needed or at least secondary.

Jeremy Farrance
  • 740
  • 6
  • 11
1

Basically 2 steps

  1. Get the real file name using 2sxc and DNN
  2. Then load the file as a string using normal .net stuff System.IO and add it to your html - https://learn.microsoft.com/en-us/dotnet/api/system.io.file.readalltext?view=netframework-4.5.1

ca. like this

<div>
 @Html.Raw(System.IO.File.ReadAllText(fileName)
</div>
iJungleBoy
  • 5,325
  • 1
  • 9
  • 21