Is there a setting to get the full domain generated for media that are included in a Rich Text Editor Field?
The useDomainPrefixes only affects urls for content and I was hoping it would be the same for the media.
I really like that idea about creating an event but I wanted to keep my change to Razor
Option 1 - use HtmlAgilityPack - I didn't use this one. It put weird little characters in my output.
@using HtmlAgilityPack
@{
var header = Umbraco.Field("headerText").ToString();
var html = new HtmlDocument();
html.OptionDefaultStreamEncoding = System.Text.Encoding.UTF8;
html.LoadHtml(header);
foreach (HtmlAgilityPack.HtmlNode img in html.DocumentNode.SelectNodes("//img").ToList())
{
img.Attributes["src"].Value = String.Format("{0}{1}", Request.Url.GetLeftPart(UriPartial.Authority), img.Attributes["src"].Value);
}
using (var ms = new MemoryStream())
{
html.Save(ms);
ms.Position = 0;
var sr = new StreamReader(ms, true);
header = sr.ReadToEnd();
}
}
Dude,
Here is the final Helper that I ended up with. Its pretty cool now. It drops in a base64 version of the image. Not recommended unless you need to do that. Notice the 2 encoding settings which are both needed to sort out the encoding stuff.
@helper RenderText(string input)
{
var output = String.Empty;
var html = new HtmlAgilityPack.HtmlDocument();
html.OptionDefaultStreamEncoding = System.Text.Encoding.UTF8;
html.LoadHtml(input);
var images = html.DocumentNode.SelectNodes("//img[contains(@src, 'media')]");
if (images != null)
{
foreach (var img in images)
{
img.Attributes["src"].Value = "data:image/gif;base64," + Convert.ToBase64String(System.IO.File.ReadAllBytes(Server.MapPath(img.Attributes["src"].Value.Split(new char[] { '?' })[0])));
}
}
using (var ms = new MemoryStream())
{
html.Save(ms);
ms.Position = 0;
var sr = new StreamReader(ms, System.Text.Encoding.UTF8);
output = sr.ReadToEnd();
}
@Html.Raw(output)
}
full domain for media
Is there a setting to get the full domain generated for media that are included in a Rich Text Editor Field? The useDomainPrefixes only affects urls for content and I was hoping it would be the same for the media.
Hi Ben,
You can try to subscribe on Create Media event and rewrite path to media. List of events :
http://our.umbraco.org/wiki/reference/api-cheatsheet/using-applicationbase-to-register-events/overview-of-all-events
Thanks
I really like that idea about creating an event but I wanted to keep my change to Razor
Option 1 - use HtmlAgilityPack - I didn't use this one. It put weird little characters in my output.
Option 2 - Good Ole Replace - I used this one.
Use them like this
i ended up working out the encoding issue. I added html.OptionDefaultStreamEncoding = System.Text.Encoding.UTF8; to the code above.
hi Ben,
So do you use HtmlAgilityPack for finding media items on the page ? Is it the better solution ?
Dude, Here is the final Helper that I ended up with. Its pretty cool now. It drops in a base64 version of the image. Not recommended unless you need to do that. Notice the 2 encoding settings which are both needed to sort out the encoding stuff.
Ben, great helper !
Thanks
is working on a reply...