https://discord.umbraco.com logo
#help-with-umbraco
How to replace an image using backoffice api
# help-with-umbraco
u

__louis___

10/16/2023, 12:38 PM
I'm trying to use the save() method of the backoffice api (https://apidocs.umbraco.com/v12/ui/#/api/umbraco.resources.mediaResource) to replace an image on an existing media item. What is the correct way to go about doing this? (I'm using v12) Any help is much appreciated.
k

kdxperbol

10/16/2023, 12:47 PM
You're doing this on the client-side "frontend" as part of the backoffice UI, right? That's what that API is for. Like in a custom property editor or similar.
u

__louis___

10/16/2023, 12:58 PM
yes
This is one of the things I've tried so far...
Copy code
js
         mediaResource.getById(1000) // get media item with image i'd like to replace
         .then(function(media) {
             mediaResource.save(media, false, [file]) // this is a File object created with the HTML File API, but I've also tried Blobs and direct image paths  
                 .then(function(media){
                     alert("Retrieved, updated and saved again");
                 })
                 .catch(function(error) {
                     console.error("Error saving media:", error);
                 });
         })
         .catch(function(error) {
             console.error("Error fetching media:", error);
         });