我有一个用 ASP.NET MVC 编写的文件上传程序。它目前在我的本地开发机器上,我想知道如何(如果可能)为每个上传的文件生成一个链接,以便在单击它时显示/下载该项目等。
处理显示文件列表的当前代码/标记:
<table>
<tr>
<th></th>
<th>
Name
</th>
<th>
Length
</th>
<th></th>
</tr>
<%
var path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "uploads");
foreach (var file in Directory.GetFiles(path))
{
var item = new FileInfo(file);
%>
<tr>
<td></td>
<td>
<%=Html.Encode(Path.GetFileName(item.Name))%>
</td>
<td>
<%=Html.Encode(item.Length >= 1024 ? item.Length / 1024 + " kilobytes" : item.Length + " bytes")%>
</td>
<td>
// This is the line in question. Does not work as-is.
<a href="<%= item.FullName %>"><%= Html.Encode(Path.GetFileName(item.Name)) %></a>
</td>
</tr>
<% } %>
</table>
我想一旦它上线,我将不得不更改文件处理代码,但现在这已经足够了。也欢迎提出建议:)
谢谢!
最佳答案
使用 Url.Content,例如:
<img src="<%= Url.Content("~/Content/UserImages/FileName.jpg") %>" />
波浪号的意思是“我网站的根,无论它在哪里。”您不必将文件放在 Content 中;您可以将它们放在站点根目录下的任何位置。
关于asp.net-mvc - 本地文件 ASP.NET/MVC 的映射路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1435363/