Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Remi Martel 32 posts 104 karma points
    Mar 09, 2018 @ 21:00
    Remi Martel
    0

    Get media Id in MediaService,Saved event

    I am trying to get the id of my image in a MediaService,Saved event so that I can get the path (path always being /media/mediaID/imagename.extension)

    This is my event:

    void MediaServiceSaved(IMediaService sender, SaveEventArgs<IMedia> e)
        {
            foreach (var page in e.SavedEntities)
            {
                //getting the image id
                int id = 0;
                foreach (var prop in page.Properties)
                {
                    if (prop.Alias == "image")
                    {
                        id = prop.Id;//!!!!! - Problem is here, this return a 0 - !!!!
                    }
                }
    
                //Path to folder containing image
                string path = "/media/" + id + "/";
    
                //check if image needs to be resized by checking if size folders exists
                if (System.IO.File.Exists(path + "tile") || System.IO.File.Exists(path + "carousel") || System.IO.File.Exists(path + "product") ||
                    System.IO.File.Exists(path + "mobileTile") || System.IO.File.Exists(path + "mobileProduct"))
                {
                    //send id to resizer to resize each variant of the image
                    Resizer resizer = new Resizer();
                    resizer.Resize(id);
                }
                //else do nothing
            }
        }
    

    enter image description here

  • Paul Tilsed 26 posts 179 karma points c-trib
    Mar 12, 2018 @ 11:42
    Paul Tilsed
    1

    Hi Remi,

    The question is a little unclear as its hard to see what you are trying to do based on just the code and its a little confusing. It looks like you are trying to loop over the properties to find an image on a media entity but since you are using the media creation event this should not be needed as you already have all that.

    You can access to the media Id by simply using the ID property on the saved entity.

            foreach (var media in e.SavedEntities)
            {
               var id = media.Id;
            }
    

    You can also use the inbuilt methods to check if the image is new and as such likely to require the crops creating on the server.

    However for functionality like this you might want to consider using the image cropper property on the page where you want to show the images. This will allow you to define the crops you want to use and then display them in a template using this simple syntax:

    <img src="@Url.GetCropUrl(Model.Content, "image", "tile")" />
    

    Paul

  • Remi Martel 32 posts 104 karma points
    Mar 12, 2018 @ 12:17
    Remi Martel
    0

    Thanks, Paul, this is exactly what I was trying to do, I just got confused and went too far in my code for no reason.

Please Sign in or register to post replies

Write your reply to:

Draft