SetMediaFileContent() Doesn't Update Existing Media Item's Content?
I am trying to write some code in an extension of UmbracoApiController which grabs image data (from a provided URL as a stream) and then, depending on whether the relevant Umbraco Member has a related MediaItem (alias 'avatar') yet or not, create or update it accordingly.
For the later, I was please to see the MediaService.SetMediaFileContent() method, which sounded like it was devised for exactly this use case.
However, from all my attempts, I cannot get this method to do anything!
It executes without complaint, but the pre-existing MediaItem content remains unchanged whatsoever. I am very confident that the second param, stream, is as expected (as my creation code runs just fine) and the first param, filepath, I am passing a string such as "/media/uafjgeov/myy_avatar.jpg" (which comes from IPublishedContent.Url())
Code Snippet to Create New Media Item (Working Fine):
// Create a new image record within the Avatars folder and assign ownership to the current member
IMedia newAvatar = Services.MediaService.CreateMedia("My Avatar", folder, Constants.Conventions.MediaTypes.Image, member.Id);
// Set the underlying media content
newAvatar.SetValue(Services.ContentTypeBaseServices, Constants.Conventions.Media.File, avatarFilename, stream);
// Save the new media item
Services.MediaService.Save(newAvatar);
Code Snippet to Update Existing Media Item (Not Working):
// Set the underlying file content of the existing avatar record
IPublishedContent existingAvatar = Umbraco.Media(existingAvatarUdi);
Services.MediaService.SetMediaFileContent(existingAvatar.Url(), stream);
I am wondering if, like the create example, this update needs persisting once made? But SetMediaFileContent() returns void so even if so, I have no IMedia object available to pass to MediaService.Save()?
Has anyone had any success in using this method or have any idea what I may be doing wrong? As usual, the Umbraco documentation is fleeting at best and provides zero examples of use in context!
SetMediaFileContent() Doesn't Update Existing Media Item's Content?
I am trying to write some code in an extension of UmbracoApiController which grabs image data (from a provided URL as a stream) and then, depending on whether the relevant Umbraco Member has a related MediaItem (alias 'avatar') yet or not, create or update it accordingly.
For the later, I was please to see the MediaService.SetMediaFileContent() method, which sounded like it was devised for exactly this use case.
See: SetMediaFileContent API Documentation
However, from all my attempts, I cannot get this method to do anything!
It executes without complaint, but the pre-existing MediaItem content remains unchanged whatsoever. I am very confident that the second param, stream, is as expected (as my creation code runs just fine) and the first param, filepath, I am passing a string such as "/media/uafjgeov/myy_avatar.jpg" (which comes from IPublishedContent.Url())
Code Snippet to Create New Media Item (Working Fine):
Code Snippet to Update Existing Media Item (Not Working):
I am wondering if, like the create example, this update needs persisting once made? But SetMediaFileContent() returns void so even if so, I have no IMedia object available to pass to MediaService.Save()?
Has anyone had any success in using this method or have any idea what I may be doing wrong? As usual, the Umbraco documentation is fleeting at best and provides zero examples of use in context!
is working on a reply...