Reputation: 169
I am trying to pass a folder path to a download controller using @Html.ActionLink
, but I am getting could not find the location error like
Could not find file 'C:\Teerth Content\Project\Colege\WebApp\Media\@item.Content'
However when I give the hard coded value it does work. May I have suggestions what is wrong with that.
Here is my code: Action method:
public FileResult Download(string fileName, string filePath)
{
byte[] fileBytes = System.IO.File.ReadAllBytes(filePath);
string documentName = fileName;
return File(fileBytes, System.Net.Mime.MediaTypeNames.Application.Octet, documentName);
}
view
@Html.ActionLink("Download", "Download", "Marketing", routeValues: new
{
fileName = @item.Content,
filePath = Server.MapPath("~/Media/@item.Content"),
area = "AffiliateAdmin"
}, htmlAttributes: null)
Upvotes: 0
Views: 2512
Reputation: 151720
Like mentioned in comments, you've got an error in your view:
The code ("~/Media/@item.Content")
renders as C:\Teerth Content\Project\Colege\WebApp\Media\@item.Content
, where you actually want Server.MapPath("~/Media/" + @item.Content)
to find the actual filename.
But you need to reconsider this design, as it opens up your entire machine to the web. Someone is bound to try Download("C:\Teerth Content\Project\Colege\WebApp\web.config", "web.config")
, exposing your connection strings and other application settings, not to mention other files on your server you really don't want clients to download.
Upvotes: 2