In this line string src = m.Groups[1].Value; it is returning the path to the image incl. querystring (width, height, crop etc). But the returned mediaIds are actually the media folder ids (in path).
So I have to take another approach. MediaService seems to have a method for that .GetMediaByPath().
Is that the only way I can get the Media or is there another way?
string src = m.Groups[1].Value;
if (!string.IsNullOrEmpty(src))
{
var mediaIndex = src.IndexOf("/media/");
var offset = src.IndexOf("?", mediaIndex + 1);
src = src.Substring(mediaIndex, offset);
}
if (!string.IsNullOrEmpty(src))
{
var ms = ApplicationContext.Current.Services.MediaService;
var media = ms.GetMediaByPath(src);
mediaIds.Add(media.Id.ToString());
}
However even the value og src is /media/43852/img_1282.jpg and by looking in Media section it has ID: 44375, but var media is returning null.
Get image inside grid editor as media
Hi..
I am looking for the first image inside grid editor and would like to return it as IPublishedContent (media).
I can find the path, but is there a way to get is as IPublishedContent, like when using Umbraco.Media(id)?
This is how I have done it for now:
In this line
string src = m.Groups[1].Value;
it is returning the path to the image incl. querystring (width, height, crop etc). But the returnedmediaIds
are actually the media folder ids (in path).So I have to take another approach.
MediaService
seems to have a method for that.GetMediaByPath()
.Is that the only way I can get the Media or is there another way?
/Bjarne
I have changed this part:
However even the value og
src
is/media/43852/img_1282.jpg
and by looking in Media section it hasID: 44375
, butvar media
is returning null.It has been reported and fixed here http://issues.umbraco.org/issue/U4-6004 but it seems to be an issue in Umbraco 7.4.1 as well.
/Bjarne
We use an "image" grid editor in grid content. So I got it working by taking a different approach in this case.
/Bjarne
is working on a reply...