【发布时间】:2022-12-13 20:16:45
【问题描述】:
我正在尝试制作一个表格,列出链接在一起的 2 种类型的文件。一种是 .mp3,另一种是 .txt 文件。我希望将这些文件链接在一起,以便当 foreach 循环通过它们时,共享相同名称的文件共享一行。这样就可以播放 mp3 文件,并可以打开相应的文本文件。
应用程序剃刀页面有一个显示文件夹中所有文件的表格,但它不考虑这两种类型的文件是否共享相同的名称。任何人都可以帮助创建一个将文件链接在一起的类,以便可以在表中调用它们吗?
这是代码。
<table class="table table-striped mb-0">
<thead>
<tr>
<th scope="col">Name</th>
<th scope="col">Actions</th>
</tr>
</thead>
<tbody>
@foreach (var file in textList)
{
<tr>
<td>
@file.Name
</td>
<td>
<span @onclick="() => PlayAudio(file.Url)"
class="text-primary oi oi-play-circle me-2" aria-hidden="true" role="button">
</span>
<span @onclick="() => DeleteAudio(file)"
class="text-danger oi oi-trash" aria-hidden="true" role="button">
</span>
<span @onclick="() => openTextFile(file)"
><button>Open</button>
</span>
</td>
</tr>
}
}
</tbody>
</table>
@code{
readonly List<TextFile> textList = new();
readonly string FolderName = "textSoundFiles";
protected override void OnInitialized()
{
var path = $"{env.WebRootPath}\\{FolderName}\\";
var files = new DirectoryInfo(path).GetFiles();
foreach (var file in files)
{
textList.Add(new TextFile
{
Name = file.Name,
Url = $"/textFiles/{file.Name}",
Path = file.FullName
});
}
}
public class TextFile
{
public string Name { get; set; }
public string Url { get; set; }
public string Path { get; set; }
}
}
我正在尝试制作一个列出文件的表格,它允许我播放列出的音频文件中的音频。我试着 目录中的 2 种文件类型,使得具有相同名称(不包括其 MIME 类型)的文件相互链接
【问题讨论】:
-
迭代
files.GroupBy(file => file.Name) -
@BrianParker 这将如何使文件链接在一起?
-
它们将按名称分组,您将迭代一个组。这些组本身将有一个子列表,在您的情况下,一个或两个项目。如果您的名称包含扩展名
.txt或.mp3,您可能必须编写一个派生属性来删除它并按其分组。 -
@BrianParker 我遇到了
System.IO.Path.ChangeExtension(path, null);这将允许我在保持路径的同时切断扩展,但它没有用。你能检查我帖子中的编辑,看看我做错了什么吗?
标签: .net blazor blazor-server-side