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.
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.
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.
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: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 inSavedEntities
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.
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 aStream
.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 :)
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 toStream
.Hope that helps someone!
is working on a reply...