Reputation: 3710
I have a simple Action which should allow me to download an asked file.
This works perfectly if I call the action using the browser's context menu (see the screenshot below), but when I click the link directly I get the following error: HTTP Error 404.0 - Not Found - The resource you are looking for has been removed, had its name changed, or is temporarily unavailable.
Using browser's context menu:
Controller Action for downloading files:
public ActionResult Download(string id)
{
string username = User.Identity.Name;
Client client = db.Client.SingleOrDefault(x => x.Email == username);
string filePath = Server.MapPath("~/Client-Documents/" + client.FolderName + "/" + id);
return File(filePath, "text/plain", id);
}
View snippet where the file links are being generated:
@for (int i = 0; i < Model.Count(); i++)
{
<tr>
<td>@(i + 1)</td>
<td>@Model[i].Name</td>
<td>
<a href="@Url.Action("Download", "Client", new { @area = "Administration", id = Model[i].Location })">Download</a>
</td>
</tr>
}
Upvotes: 0
Views: 2517
Reputation: 32694
You were passing the filename which included the extension, so it was being ignored by ASP.NET and treated as a static file, thus resulting in a 404. To avoid this, don't pass the extension. Probably best to refer to the files by ID.
Also note that if the files are part of your data, it's probably best to store them in a database. This avoids many file system pitfalls.
Upvotes: 2