I have created a lot of members in my new 7.2.2 website using the MemberService from the backend. The members have extra properties, among them a "facePhoto". I used the MediaService to upload them and I can see them just fine on the members page in the back office.
How do I view these images from a razor macro? Using MemberService I can get a list of members. I can get the Media object just fine as well:
@if (m.HasProperty("facePhoto"))
{
var imageId = m.GetValue<int>("facePhoto");
if (imageId > 0)
{
var imageInfo = mediaService.GetById(imageId);
var imagePath = imageInfo.Path;
}
}
The variable imageInfo is of type Umbraco.Core.Models.Media and contains the filename I expect. The problem is that the imagePath is a set of not very useful numbers ( for example -1,1092,1196). I can find nothing online about this latest version. Many examples cite Umbraco.Media but this seems not to exist in the current version. I feel sure this should be easy but so far it is not at all.
the path is only the path of media node ids in the media section. If you use the default mediatype "image" you can get the default umbraco property "umbracoFile". This property contains the url of your media file.
that has not work. But our.umbraco.org was just short unavailable. After you have do this you should see the post in a green box. You can click to do this on the green hook in the post with the solution.
I was so relieved to get a solution yesterday that I did not pause to think. And in fact the solution is too much like magic for my taste. I accept that I need to remember my own type aliases (at least for now) but why should I have to remember "umbracoFile"? Anyone who uses MediaService is going to want to get a usable path the their images, that is what the service is for, surely?
I have written an extension method on MediaService to use for now but await an improved version in 7.3 or whenever.
public static string GetImagePathById(this IMediaService service, int id)
{
if (id > 0)
{
var imageInfo = service.GetById(id);
if (imageInfo != null)
{
return imageInfo.GetValue<string>("umbracoFile");
}
}
return string.Empty;
}
This is not great, as it assumes the id refers to an image and that nothing will go wrong. But it is good enough for my purposes at present.
MediaService and MemberService in Umbraco 7.2.2
Hi,
I have created a lot of members in my new 7.2.2 website using the MemberService from the backend. The members have extra properties, among them a "facePhoto". I used the MediaService to upload them and I can see them just fine on the members page in the back office.
How do I view these images from a razor macro? Using MemberService I can get a list of members. I can get the Media object just fine as well:
@if (m.HasProperty("facePhoto"))
{
var imageId = m.GetValue<int>("facePhoto");
if (imageId > 0)
{
var imageInfo = mediaService.GetById(imageId);
var imagePath = imageInfo.Path;
}
}
The variable imageInfo is of type Umbraco.Core.Models.Media and contains the filename I expect. The problem is that the imagePath is a set of not very useful numbers ( for example -1,1092,1196). I can find nothing online about this latest version. Many examples cite Umbraco.Media but this seems not to exist in the current version. I feel sure this should be easy but so far it is not at all.
Oh, and Umbraco.TypedMedia isn't available either. Not that that is an obvious answer.
Hi AmandaEly,
the path is only the path of media node ids in the media section. If you use the default mediatype "image" you can get the default umbraco property "umbracoFile". This property contains the url of your media file.
Best,
Sören
With this code you should get the url:
Best,
Sören
Sören, my part-Norwegian heart thrill to your name! And yes, that got it in one,. Whew! I know I shoudl be using MediaService.
thanks,
Amanda
Hi Amanda,
great that I could help you :-) Can you mark the post with the solution above as solved please? So other people can easier find the solution.
Best, Sören
I think I have now done this.
Hi Amanda,
that has not work. But our.umbraco.org was just short unavailable. After you have do this you should see the post in a green box. You can click to do this on the green hook in the post with the solution.
Cheers,
Sören
Hi Sören,
I was so relieved to get a solution yesterday that I did not pause to think. And in fact the solution is too much like magic for my taste. I accept that I need to remember my own type aliases (at least for now) but why should I have to remember "umbracoFile"? Anyone who uses MediaService is going to want to get a usable path the their images, that is what the service is for, surely? I have written an extension method on MediaService to use for now but await an improved version in 7.3 or whenever.
This is not great, as it assumes the id refers to an image and that nothing will go wrong. But it is good enough for my purposes at present.
Amanda
Hi Amanda,
"umbracoFile" is the default de-facto standard in umbraco for the alias of media type upload fields.
Best,
Sören
is working on a reply...