如何将文件和文件路径从 C# 控制器发送到视图
Posted
技术标签:
【中文标题】如何将文件和文件路径从 C# 控制器发送到视图【英文标题】:How send File and File Path from C# Controller to the View 【发布时间】:2022-01-16 14:46:44 【问题描述】:我正在将文件和文件路径从控制器发送到 MVC 视图:
这是我的控制器:
public ActionResult Help()
var releaseNoteFiles = Directory.GetFiles(Server.MapPath("~/Content/ReleaseNotes"));
List<string> month = new List<string>();
foreach (var releaseNoteFile in releaseNoteFiles)
month.Add(new Regex("([^A-Z]*)([a-zA-Z]*)").Match(Path.GetFileNameWithoutExtension(releaseNoteFile).Split('.').Last()).Groups[2].Value);
ViewBag.releaseNoteFilesmonth = month;
ViewBag.releaseNoteFiles = releaseNoteFiles;
return View();
这是在视图中显示的方式:
@foreach (var item in ViewBag.releaseNoteFilesmonth)
<a href="(ViewBag.releaseNoteFiles as string[]).Where(x => x.Contains(item)).FirstOrDefault()"> @item</a>
<br />
这是我期待的输出:
OCT - (Link to the file)
NOV - (Link to the file)
Dec - (Link to the file)
当我像这样对路径进行硬编码时,它正在工作:
href="~/Content/ReleaseNotes/Release Notes 1.104.0_August102021.pdf"
但是当我尝试动态访问它时不确定如何写入文件的路径,这一行无法正常工作:
href="(ViewBag.releaseNoteFiles as string[]).Where(x => x.Contains(item)).FirstOrDefault()">
【问题讨论】:
【参考方案1】:Server.MapPath
返回驱动器的文件路径,例如E://projectfolder/content/...
并且不要在url中使用这个地址。
将此行添加到您的代码并设置为ViewBag.releaseNoteFiles = filePath;
List<string> filePath = releaseNoteFiles.Select(a => string.Format("0/1", "~/Content/ReleaseNotes", Path.GetFileNameWithoutExtension(a))).ToList();
完整代码
public ActionResult Help()
var releaseNoteFiles = Directory.GetFiles(Server.MapPath("~/Content/ReleaseNotes"));
List<string> month = new List<string>();
List<string> filePath = releaseNoteFiles.Select(a => string.Format("0/1", "~/Content/ReleaseNotes", Path.GetFileNameWithoutExtension(a))).ToList();
foreach (var releaseNoteFile in releaseNoteFiles)
month.Add(new Regex("([^A-Z]*)([a-zA-Z]*)").Match(Path.GetFileNameWithoutExtension(releaseNoteFile).Split('.').Last()).Groups[2].Value);
ViewBag.releaseNoteFilesmonth = month;
ViewBag.releaseNoteFiles = filePath;
return View();
或使用:
public ActionResult Help()
var releaseNoteFiles = Directory.GetFiles(Server.MapPath("~/Content/ReleaseNotes"));
ViewBag.releaseNoteFilesmonth = releaseNoteFiles
.Select(a => new Regex("([^A-Z]*)([a-zA-Z]*)").Match(Path.GetFileNameWithoutExtension(a).Split('.').Last()).Groups[2].Value).ToList();
ViewBag.releaseNoteFiles = releaseNoteFiles.Select(a => string.Format("0/1", "~/Content/ReleaseNotes", Path.GetFileNameWithoutExtension(a))).ToList();
return View();
并将视图更改为:
@foreach (var item in ViewBag.releaseNoteFilesmonth)
string url = string.Empty;
if(ViewBag.releaseNoteFiles != null && !string.IsNullOrEmpty(item))
url = (ViewBag.releaseNoteFiles as string[]).Where(x => x.Contains(item)).FirstOrDefault();
<a href="@url"> @item</a>
<br />
【讨论】:
谢谢,我在控制器中添加了这段代码,但是视图中的代码不起作用,我应该在 href="" 修改你的视图代码。我把视图代码放在响应中 谢谢@Meysam Asadi,收到此错误:值不能为空。参数名:source on the string url = .... in view 通常不应该给出这样的错误。 ViewBag.releaseNoteFiles 可能为空 它在我调试时有价值以上是关于如何将文件和文件路径从 C# 控制器发送到视图的主要内容,如果未能解决你的问题,请参考以下文章
如何从 C# WPF 中的嵌入字体将字体文件添加到 Stimulsoft 报告
如何使用 API 和 c# 从 azure DevOps 服务器下载文件到指定路径
从 C# 应用程序将文件发送到 Symantec AV Scan Engine