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>";
}
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.
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>";
}
}
}
<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>
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?
Should be something like child.Url()
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:
never used the extension, but
child.Url()
should work, if not trychild.MediaUrl()
Neither of those methods exist for an IMedia object.
Sorry, thought you were using published content, try the code here
https://our.umbraco.com/forum/using-umbraco-and-getting-started/109242-how-to-get-media-using-mediaservice
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:
And changed it to:
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:
Scripts:
Style:
View:
is working on a reply...