The controller comes with a Filecontentresult that allows us to easily return files to the server, reducing the number of steps. To download files, such as video, text, images such as browser-supported files, the default will be opened by the browser. When you want to make it a download, you also need to set up a response header to tell the browser how to handle it.
Public ActionResult DownLoadFile (string path, String fileName, String contentType) { Response.AddHeader (" Content-disposition "," attachment; Filename= "+ server.urlencode (fileName)); Return File (Path,contenttype); }
Above, in JS using location.href = Filedownloadurl Such a simple way can be implemented in the ASP. NET mvc5 download file. The key is this content-disposition, and its content is about how to handle it, and what the default file name is. Attachment is the attachment to the meaning of processing, followed by the file name, URL encoding processing can be avoided because of the file name with a space, and so on.
Aside from this, this is done by setting the response header to ensure that the file is downloaded as an attachment as early as a year ago, when I saw a face, and did not remember the detailed understanding, and now used, vaguely remember as if used once, of course, do not remember how to write, online search, and can not find, Or you have to knock down a yard by one word, so impressive and easy to find later. What reprint ah, copy and paste are floating clouds, who Code who know!
ASP. NET mvc5 Download File method