umbraco replace flle with same name in media picker

407 Views Asked by At

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

There are 1 best solutions below

0
f33n3y On

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
        }

    }
}