如何使用ASP.NET MVC 1.0从文件系统直接提供文件?

Mik*_*osa 4 asp.net-mvc

我有一个在Windows Server 2003 IIS 6.0上运行的ASP.NET MVC 1.0应用程序.

我刚刚添加了一项新功能,允许用户将文件上传到服务器.我还添加了一个页面,显示该用户上传的文件列表.

问题是当有人点击查看文件时,我收到以下错误:系统找不到指定的文件.

我已经验证了一切都是正确的,我无法想象我的生活.

我将此代码添加到路由部分,认为可能与它有关,但它没有帮助.

routes.RouteExistingFiles = false;
routes.IgnoreRoute("App_Data/Uploads/{*pathInfo}");
Run Code Online (Sandbox Code Playgroud)

任何帮助将不胜感激.

Dar*_*rov 6

App_Data客户端无法直接访问存储在文件夹中的文件.ASP.NET阻止访问它.所以不需要为这个特殊文件夹添加任何忽略路由,你不能使用这样的URL /App_Data/Uploads/foo.txt.如果要从此文件夹提供文件,则需要编写控制器操作,将从物理位置读取文件并将其返回给客户端:

public ActionResult Download(string id)
{
    // use the id and read the corresponding file from it's physical location
    // and then return it: 
    return File(physicalLocation, mimeType);
}
Run Code Online (Sandbox Code Playgroud)

然后使用:

<%= Html.ActionLink("download report", "download", new { id = 123 }) %>
Run Code Online (Sandbox Code Playgroud)