【问题标题】:Umbraco: Is there a way to get the list of folder under the Media (Physical Folder)Umbraco:有没有办法获取媒体下的文件夹列表(物理文件夹)
【发布时间】:2018-04-01 08:05:15
【问题描述】:

我只是想在媒体文件夹(物理文件夹)下创建一个文件夹列表(仅限文本值)。

媒体文件夹位于根文件夹。(见图)

我尝试了以下代码只是为了测试我是否可以获得最后一个文件夹:

var contentService = Services.ContentService;
var folderList = contentService.GetRootContent().Last();
IContent s = folderList;

string nameOfLastFolder = folderList.Name.ToString();

但它返回了主页

我也试过下面的代码:

var mediaService = Services.MediaService;
var folderList = mediaService.GetRootMedia().Last();
IMedia s = folderList;

string nameOfLastFolder = folderList.Name.ToString();

但如果我要获取的是后台媒体而不是物理媒体,它会返回最后一个媒体文件的名称,这是正确的。 (见下图)

我想要的是物理上的媒体文件夹列表,而不是在后台查看媒体。我想创建一个物理媒体文件夹列表:

我希望你能帮助我解决这个问题。谢谢。

【问题讨论】:

  • 你为什么不只是查看文件系统? string pathToFiles = HttpServerUtility.MapPath("/Media"); string[] fileList = Directory.GetFiles(pathToFiles);
  • 正如 Eyescream 所说,您应该使用文件系统。 Umbraco 内容和媒体服务在数据库中查找节点,而不是实际文件。
  • 是的,正确,我知道那些内容和媒体与物理文件中的实际文件本身没有任何关系。我会试试那个代码。非常感谢您的回复。 :-)
  • 我猜 HttpServerUtility 在我尝试编写时没有名为 MapPath() 的方法。我在网上搜索,正如我所见,确实有一个 MapPath() 方法,但它似乎对我不起作用。当我写 HttpServerUtility 时,只有 4 个可用的方法,没有别的了。

标签: asp.net content-management-system umbraco


【解决方案1】:

您应该遍历文件系统中的文件夹,而不是使用 Umbraco 服务(列出数据库条目)。

例子

string pathToFiles = HostingEnvironment.MapPath("/Media");
string[] fileList = Directory.GetFiles(pathToFiles);

【讨论】:

  • 非常感谢。它现在工作正常。 string pathToFiles = HostingEnvironment.MapPath("/Media"); string[] fileList = Directory.GetDirectories(pathToFiles);
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-11
  • 1970-01-01
  • 1970-01-01
  • 2018-06-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多