Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Björn 18 posts 158 karma points
    Mar 13, 2018 @ 15:38
    Björn
    0

    Save images to local folder on upload

    I'm using a js library to display 3D models on the web. The library requires the texture images to be put in the same folder but since Umbraco uploads each image into a seperate folder on disc I need to create an eventhandler that copies the images and saves them to a different folder. I've created my eventhandler on file upload but how do I go about saving the uploaded images to another local directory?

    /Björn

        public class RegisterEvents : ApplicationEventHandler
    {
        protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
        {
            MediaService.Saved += MediaServiceSaved;
        }
    
        void MediaServiceSaved(IMediaService sender, SaveEventArgs<IMedia> e)
        {
            foreach (var mediaItem in e.SavedEntities)
            {
                var parent = mediaItem.Parent();
                var grandParent = parent.Parent();
                var mediaFolde = grandParent.Parent();
    
                while (parent.Parent() != null)
                {
                    parent = parent.Parent();
                }
    
                /*If top parent folder id is 1126 we know that it's a 3D model that's being uploaded 
                 and we want to save the images to a different folder.*/ 
                if (parent.Id == 1126)
                {
                    var ms = ApplicationContext.Current.Services.MediaService;
                    var folder = mediaItem.Parent();
                    var folderId = folder.Id;
    
                    string path = @"C:\Dev\Umbraco\EntreSyd\EntreSyd\3dContent\" + folderId.ToString();
    
                    if (Directory.Exists(path))
                    {
                        //Save images in folder
                    }
                    else
                    {
                        //Create folder and save images to it                      
                    }
                }
            }
        }
    }
    
  • Björn 18 posts 158 karma points
    Mar 15, 2018 @ 15:14
    Björn
    100

    Never mind. I just copied the files on disc to the location I wanted the images using c#.

Please Sign in or register to post replies

Write your reply to:

Draft