If the file is not inside your application folders and is not accessible directly from the client, you can have a controller action that will transfer the contents of the file to the client. This can be achieved by returning a FileResult from your controller action using the File method:
public ActionResult Download() { string file = @"c:\someFolder\foo.xlsx"; string contentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; return File(file, controntType, Path.GetFileName(file)); }
and then replace your button with an anchor pointing to this controller action:
@Html.ActionLink("Button 1", "Download", "SomeController")
As an alternative to using an anchor, you can also use the html form:
@using (Html.BeginForm("Download", "SomeController", FormMethod.Post)) { <button type="submit">Button 1</button> }
If the file is inside some not accessible from the client folder of your application, such as App_Data , you can use MapPath to build the full physical path to this file using the relative path:
string file = HostingEnvironment.MapPath("~/App_Data/foo.xlsx");
source share