Nepethya Rana

Nepethya Rana

  • NA
  • 335
  • 152.9k

How do I download file from the server in MVC

Oct 26 2017 10:23 AM
Previously file was located in the app folder so that I had given file path in anchor tag.
 
<div style="color:#413f5a;padding:10px">
<a href="/MyApplication/MyAppFolder/MyFile.xls">
<i class="glyphicon glyphicon-file" style="font-size:20px"></i>
<span style="font-size:15px;margin-left:8px">MyFile.xls</span>
</a>
</div> 
 
Now this file will be placed in different folder in server. I need to download/open the file.
 
What i have tried is
 
@Html.ActionLink("MyFile.xls", "DownloadFile", "Home")
 

public FileResult Download()
{
byte[] fileBytes = System.IO.File.ReadAllBytes(@"\\ip-myserver-101\MyFolder\MyFile.xls");
string fileName = "MyFile.xls";

return File(fileBytes, System.Net.Mime.MediaTypeNames.Application.Octet, fileName);
}
 
But it did not work. I got error message .
 

Server Error in '/' Application.

The resource cannot be found.

Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.

Requested URL: /Home/DownloadFile 

Answers (3)