Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How Do I Serve Files Direct From the FileSystem Using ASP.NET MVC 1.0?

Tags:

asp.net-mvc

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.

like image 840
Mike Roosa Avatar asked Feb 24 '23 02:02

Mike Roosa


2 Answers

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 }) %>
like image 109
Darin Dimitrov Avatar answered Feb 27 '23 05:02

Darin Dimitrov


Try adding ignore routes (so that they're just served without going through routing)

routes.IgnoreRoute("{file}.txt");
like image 27
BlackTigerX Avatar answered Feb 27 '23 05:02

BlackTigerX