Copied to clipboard

Flag this post as spam?

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


  • Jos Huybrighs 23 posts 55 karma points
    Aug 26, 2014 @ 14:24
    Jos Huybrighs
    1

    MediaService 'Saved' event invoked twice

    I recently upgraded to umbraco 6.2.1 and decided to use the what I understand now recommended way of attaching event handlers for documents and media in 6.2 and 7.

    So I used the technique described in MediaService-Events to hook up my media event handler (which simply has to scale down any uploaded image).

    In my previous implementations (using the 'old Media.BeforeSave concept) I always looked at the "umbracoFile" property to get the file location of the saved image and used that to create a reduced version. Now surprisingly,this doesn't immediately work with the new event handling.

    It turns out that the 'Saved' event is called twice:
    - The first time, none of the media properties are available.
    - The second time all property values are correctly assigned.

    Luckily, there is an IMedia method 'IsNewEntity()', which is set true the first time and false the second time, to get the job done, but I am wondering why 'Saved' is actually called twice.

  • Roald Haverkamp 2 posts 23 karma points
    Mar 10, 2015 @ 12:46
    Roald Haverkamp
    1

    Hi Jos,

    I was wondering if you ever figured out why the Saved event is called twice?

    Gr Roald

  • Jos Huybrighs 23 posts 55 karma points
    Mar 17, 2015 @ 18:15
    Jos Huybrighs
    1

    Roald,

    No, I didn't investigate this more deeply. I'am now always calling 'IsNewEntity()' on an IMedia object and only deal with these objects when the method returns false.

    Greetings, Jos

  • Matt Speakman 11 posts 44 karma points
    Sep 08, 2015 @ 09:13
    Matt Speakman
    1

    I get a similar problem with Version 7.2.5 I hook into the MediaService.Saving event to scale down an image as well. The first time media.GetValue<string>("umbracoFile") returns a valid path to the image whereas the second time it returns a Json object. I have had to try to parse it into a JObject in order to return a valid path from .src. Otherwise the whole thing errors out with "Illegal Characters in path"

    if (!String.IsNullOrEmpty(media.GetValue<string>("umbracoFile")))
    {
        path = media.GetValue<string>("umbracoFile");
    }
    
    try
    {
        dynamic jsonPath = JObject.Parse(path);
        if (jsonPath.src != null)
        {
            path = jsonPath.src.ToString();
        }
    
    }
    catch(JsonReaderException) { 
        //Do nothing as it will have been a normal IMedia object.
    }
    
Please Sign in or register to post replies

Write your reply to:

Draft