How Do I Serve Files Direct From the FileSystem Using ASP.NET MVC 1.0?
I have an ASP.NET MVC 1.0 application running on Windows Server 2003 IIS 6.0. 开发者_高级运维
I just added a new feature that lets users upload files to the server. I also added a page that displays a list of the files uploaded by that user.
The problem is when someone clicks to view the file, I get the following error: The system cannot find the file specified.
I have verified everything is correct and I can't figure this out for the life of me.
I added this code to the routing section thinking that may have something to do with it but it hasn't helped.
routes.RouteExistingFiles = false;
routes.IgnoreRoute("App_Data/Uploads/{*pathInfo}");
Any help would be greatly appreciated.
Files stored in App_Data
folder cannot be directly accessed by clients. ASP.NET blocks acces to it. So no need to add any ignore routes for this special folder, you cannot use an url like this /App_Data/Uploads/foo.txt
. If you want serving files from this folder you need to write a controller action will read the file from the physical location and return it to the client:
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);
}
and then use:
<%= Html.ActionLink("download report", "download", new { id = 123 }) %>
Try adding ignore routes (so that they're just served without going through routing)
routes.IgnoreRoute("{file}.txt");
What is the exact file type that is being downloaded? IIS6 and higher by default blocks unknown MIME types from being downloadable. This prevents a possible security issue where a developer or site administrator inadvertantly leaves some files laying around that could contain sensitive info.
Here's a KB article on how to enable downloading files: http://support.microsoft.com/default.aspx?scid=kb;en-us;326965
BTW you shouldn't need to change any route settings. The default route settings enable all file downloads (assuming IIS is configured to allow it).
精彩评论