Copied to clipboard

Flag this post as spam?

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


  • DG 8 posts 119 karma points
    Oct 10, 2022 @ 16:48
    DG
    0

    Retrieve media library file binary/image bytes in 'MediaSavingNotification' handler

    I have an INotificationHandler<MediaSavingNotification> which I'm hooking into to work with uploaded images, example:

    internal class MediaSavingHandler : INotificationHandler<MediaSavingNotification>
    {
        public void Handle(MediaSavingNotification notification)
        {
            if (notification.SavedEntities == null || !notification.SavedEntities.Any())
            {
                return;
            }
    
            foreach (var entity in notification.SavedEntities)
            {
                  // Get image bytes here
            }
        }
    }
    

    I'd like to access the uploaded image files bytes in a simple and easy way, is that possible?

    It seems that the interface implementation provides you with a collection of IMedia objects in SavedEntities which would be the uploaded files but I don't see anything obvious on this type that would give me what I need.

    I have this working in a crude fashion which is to fetch the image URL and request the file via HttpClient but I feel there must be a better way to do this.

    Any help is appreciated, thanks.

  • Roy Berris 89 posts 576 karma points c-trib
    Oct 13, 2022 @ 11:22
    Roy Berris
    100

    Hi,

    There is a difference between IMedia (basically the content item) that is available in umbraco under the Media section. This is the media object. The media itself, if this is an image, video or document (pdf for example) is not sent along in the notification.

    You could definitely get this data with IMediaService.GetMediaFileContentStream which gives you a Stream.

  • DG 8 posts 119 karma points
    Oct 13, 2022 @ 14:45
    DG
    0

    Just came by to update the thread to say that I'd found IMediaService.GetMediaFileContentStream and you beat me to it!

    Appreciate you taking the time to get back to me :)

  • Simon Campbell 26 posts 348 karma points
    Nov 30, 2023 @ 15:21
    Simon Campbell
    0

    In case anyone else stumbles on this thread, I found that you can use item.GetValue<long>("umbracoBytes") to get the file size, without having to resort to Stream.

    Hope that helps someone!

Please Sign in or register to post replies

Write your reply to:

Draft