One of the views on my MVC web app has URLs that allow users to view specific text files stored on the server machine. Here’s the associated controller function (assume path is defined already):
Public Function DownloadResults() As ActionResult
Return File(path, "text/plain")
End Function
The above is called by something simple, namely <%=Html.ActionLink("View File", "DownloadResults")%>, within the view. When the user clicks on the View File URL, they are redirected to a URL where the text file’s contents are printed on a page in the browser.
What I’d like to do, however, is pop up a dialog box asking if the user would like to download the file, and upon confirmation, download a physical copy of the .txt to their Downloads folder. What’s the best way to achieve this?
You need to set attachments in the response header. For this purpose you can crate
ActionResultfor example:by Phill Haack: http://haacked.com/archive/2008/05/10/writing-a-custom-file-download-action-result-for-asp.net-mvc.aspx