Copied to clipboard

Flag this post as spam?

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


  • Amir Ahmed 5 posts 85 karma points
    Mar 23, 2021 @ 10:14
    Amir Ahmed
    0

    Need to upload a series of images into the same folder in umbraco

    I'm trying to upload a series of images into the same folder in umbraco but it seem to give each image a separate sub folder. Does anyone know how this can achieved easily in Umbraco. I want to avoid using ftp to transfer the files.

    I'd like it to be like this.

    /media/folder1/image-series-01.jpg

    /media/folder1/image-series-02.jpg

    /media/folder1/image-series-03.jpg

    instead of this.

    /media/xkjsakds/image-series-01.jpg

    /media/paosdas/image-series-02.jpg

    /media/xcswves/image-series-03.jpg

  • Huw Reddick 1737 posts 6098 karma points MVP c-trib
    Mar 23, 2021 @ 12:24
    Huw Reddick
    0

    You could do this in code and upload them wherever you wish, but if you are doing it from the back office then this is how umbraco stores it's files, not sure if it can be over-ridden or not

  • AddWeb Solution Pvt. Ltd 109 posts 360 karma points
    Mar 24, 2021 @ 04:47
    AddWeb Solution Pvt. Ltd
    1

    Hello Amir,

    Try this :-

    private static void Media_AfterSave(Media sender, SaveEventArgs e)
    {
    string fullImagePath = sender.getProperty("umbracoFile").Value.ToString();
    string fullParentImagePath = "";
    Media parentMedia = new Media(sender.Parent.Id);
    string sourceFile = HttpContext.Current.Request.MapPath(fullImagePath);
    var fileName = Path.GetFileName(fullImagePath);
    //parent already has an image created
    if (parentMedia.getProperty("umbracoFile").Value.ToString() != "")
    {
    fullParentImagePath = parentMedia.getProperty("umbracoFile").Value.ToString();
    var destinationFolder = Path.GetDirectoryName(fullParentImagePath);
    string destinationPath = HttpContext.Current.Request.MapPath(destinationFolder);
    //delete file is already exists
    if (File.Exists(Path.Combine(destinationPath, fileName)))
    File.Delete(Path.Combine(destinationPath, fileName));
    //copy file
    File.Copy(sourceFile, Path.Combine(destinationPath, fileName));
    }
    else
    {
    int parentFolderId = parentMedia.getProperty("umbracoFile").Id;
    string mediaPath = "/media/" + parentFolderId + "/";
    string parentPath = HttpContext.Current.Request.MapPath(mediaPath);
    //create directory
    if (!Directory.Exists(parentPath))
    {
    Directory.CreateDirectory(parentPath);
    }
    string newFilePath = Path.Combine(parentPath, fileName);
    //copy file into directory
    File.Copy(sourceFile, newFilePath);
    //set property value for initial image
    parentMedia.getProperty("umbracoFile").Value = mediaPath;
    }
    
    }
    
Please Sign in or register to post replies

Write your reply to:

Draft