Copied to clipboard

Flag this post as spam?

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


  • Ross Ekberg 124 posts 365 karma points
    Aug 24, 2023 @ 13:59
    Ross Ekberg
    0

    Get Media URL

    Umbraco 10 (.NET Core 6)

    I am trying to get the url of a PDF that has been uploaded to the media folder, and to display it as a link in a list. I knew how to do this in Umbraco 7, but I can't figure it out in Umbraco 10. Below is the code in my controller. It uses recursion in order to create an unordered list of links. Below, the "child" is of type IMedia. Everything works just fine, I just can't figure out how to get the url of the file. A couple other questions I had, is there a simpler way of doing this? Is there a better way than using the "GetPagedChildren"? I chose a random page size of 500. Is that okay?

        public void GetBranch(ref string output, IMedia media, bool top)
        {
            output += "<ul";
            if (!top)
                output += " class='nested'";
            output += ">";
    
            var children = mediaService.GetPagedChildren(media.Id, 0, 500, out var total);
    
            foreach(var child in children)
            {
                output += "<li>";
                if(child.ContentType.Alias == "Folder")
                {
    
                    output += "<span class='caret'>" + child.Name + "</span>";
                    GetBranch(ref output, child, false);
                } else
                {
                    output += "<a href='" + <THIS IS WHERE I NEED TO GET THE URL>+ "'>" + child.Name + "</a>";
                }
                output += "</li>";
            }
            output += "</ul>";
        }
    
  • Huw Reddick 1929 posts 6697 karma points MVP 2x c-trib
    Aug 24, 2023 @ 14:34
    Huw Reddick
    0

    Should be something like child.Url()

  • Ross Ekberg 124 posts 365 karma points
    Aug 24, 2023 @ 14:39
    Ross Ekberg
    0

    There is an extension "GetUrl()" but I don't know how to use it. Here is it's details. Maybe you could help me figure it out:

    (extension) string? GetUrl(string PropertyAlias, Umbraco.Cms.Core.PropertyEditors.MediaUrlGeneratorCollection mediaUrlGenerators)
    
  • Huw Reddick 1929 posts 6697 karma points MVP 2x c-trib
    Aug 24, 2023 @ 16:08
    Huw Reddick
    0

    never used the extension, but child.Url() should work, if not try child.MediaUrl()

  • Ross Ekberg 124 posts 365 karma points
    Aug 24, 2023 @ 16:11
    Ross Ekberg
    0

    Neither of those methods exist for an IMedia object.

  • Huw Reddick 1929 posts 6697 karma points MVP 2x c-trib
    Aug 24, 2023 @ 16:16
    Huw Reddick
    0
  • Ross Ekberg 124 posts 365 karma points
    Aug 24, 2023 @ 17:14
    Ross Ekberg
    103

    Yea, I had found that one already. I couldn't get it to work. However, I just did some picking-apart on it and I found a simplified version of a key line actually worked.

    I took this line:

    dynamic umbracoFile = JsonConvert.DeserializeObject(image.GetValue<string>(Constants.Conventions.Media.File));
    

    And changed it to:

    string url = image.GetValue<string>(Constants.Conventions.Media.File);
    

    Thank you your encouragement.

    For anyone else out there, here is my working code. This will iterate through the file tree, starting with a root folder of your choice (by ID), and create an unordered list of folders and files, with links to the files, that is collapsible.

    Controller:

        using Microsoft.AspNetCore.Mvc;
        using Umbraco.Cms.Core;
        using Umbraco.Cms.Core.Models;
        using Umbraco.Cms.Core.Services;
    
        namespace Prod.Controllers    
        {
            [Route("[controller]/[action]")]
            public class MyController : Controller
            {
                private readonly IMediaService mediaService;
    
                public MyController(IMediaService mediaService)
                {
                    this.mediaService = mediaService;               
                }           
    
    
                [HttpPost]
                public ActionResult GetTree()
                {
                    LoadCurrentTree();
    
                    return PartialView("My/View");
                }
    
                public void LoadCurrentTree()
                {
                    var output = "";
                    var folder = mediaService.GetById(1250);
    
                    GetBranch(ref output, folder, true);
    
                    ViewBag.CurrentTree = output;
                }
    
                public void GetBranch(ref string output, IMedia media, bool top)
                {
                    output += "<ul";
                    if (!top)
                        output += " class='nested'";
                    output += ">";
    
                    var children = mediaService.GetPagedChildren(media.Id, 0, 100, out var total);
    
                    foreach(var child in children)
                    {                    
                        output += "<li>";
                        if(child.ContentType.Alias == "Folder")
                        {
    
                            output += "<span class='caret'>" + child.Name + "</span>";
                            GetBranch(ref output, child, false);
                        } else
                        {                        
                            string url = child.GetValue<string>(Constants.Conventions.Media.File);       
    
                            output += "<a href='" + url + "' target='_blank'>" + child.Name + "</a>";
                        }
                        output += "</li>";
                    }
                    output += "</ul>";
                }
           }
    }
    

    Scripts:

    <script type="text/javascript">
        $(function () {        
            $(".caret").click(function(){   
                $(this).next(".nested").toggleClass("active");
                $(this).toggleClass("caret-down");
            });
        });
    </script>
    

    Style:

    <style>
        /* Remove default bullets */
        ul {
          list-style-type: none;
        }
    
        /* Style the caret/arrow */
        .caret {
          cursor: pointer;
          user-select: none; /* Prevent text selection */
        }
    
        /* Create the caret/arrow with a unicode, and style it */
        .caret::before {
          content: "\25B6";
          color: black;
          display: inline-block;
          margin-right: 6px;
        }
    
        /* Rotate the caret/arrow icon when clicked on (using JavaScript) */
        .caret-down::before {
          transform: rotate(90deg);
        }
    
        /* Hide the nested list */
        .nested {
          display: none;
        }
    
        /* Show the nested list when the user clicks on the caret/arrow (with JavaScript) */
        .active {
          display: block;
          padding-bottom: 20px;
        }
    </style>
    

    View:

    @Html.Raw(ViewBag.CurrentTree)
    
Please Sign in or register to post replies

Write your reply to:

Draft