0
votes

How can I extend Umbraco media picker to overwrite the file if same name file is uploaded?

enter image description here

In image above It creates two file but I want first one to be replaced with new file.

I am using umbraco 7.11.1

1
To my knowledge, there isn't a clean already-built solution to this, probably due to how media is stored behind the scenes (against id, rather than as a tree). If you're inclined to try your own fix it might be worth trying to hijack the upload call in Angular and run a filename check to create your own prompt.Mike B
Hey, I think that you have to go in the media tab in Umbraco and click on the actual file on the tree (on the left). Right under the file it should have a 'remove file' link with a red 'x' on the left that will let you remove the file and add a different one, which basically replaces the file, just not by uploading one with the same name. I think it's intended.Jabberwocky
Thanks @Jabberwocky, Yes, that is one way. but some users don't have access to media tab and only allowed to upload from the content page.Pragnesh Patel
If you go in 'Content' and hover on the selected image a small pencil like icon comes up. That should allow you to replace the image the way I explained before. I'm not sure what happens if the user has only content tab privilegesJabberwocky

1 Answers

0
votes

Have you tried creating an ApplicationEventHandler and hooking into one of the media events listed here: https://our.umbraco.com/Documentation/Reference/Events/MediaService-Events Then you could compare the file the user is attempting to upload vs what is already in the media cache.

e.g.

public class MediaSaving : ApplicationEventHandler
{

    protected override void ApplicationStarted(UmbracoApplicationBase 
   umbracoApplication, ApplicationContext applicationContext)
    {
        MediaService.Saving += MediaServiceSaving;
    }

    void MediaServiceSaving(IMediaService sender, SaveEventArgs<IMedia> evt)
    {
        foreach (var mediaItem in evt.SavedEntities)
        {
            //Check if a new upload and correct type etc
            //Compare file paths and overwrite if appropriate
        }

    }
}