In controller, we can use fileresult to send files to the client.
Fileresult
Fileresult is an abstract class inherited from actionresult. In system. Web. MVC. dll, it has three sub-classes that send files to clients in different ways.
In actual use, we usually do not need to instantiate a fileresult subclass directly, because the Controller class already provides six file methods to simplify our operations:
Protected internal filepathresult file (string filename, string contenttype ); Protected internal virtual filepathresult file (string filename, string contenttype, string filedownloadname ); Protected internal filecontentresult file (byte [] filecontents, string contenttype ); Protected internal virtual filecontentresult file (byte [] filecontents, string contenttype, string filedownloadname ); Protected internal filestreamresult file (Stream filestream, string contenttype ); Protected internal virtual filestreamresult file (Stream filestream, string contenttype, string filedownloadname ); |
Filepathresult
Filepathresult directly sends the files on the disk to the browser:
1. The simplest way
Public actionresult filepathdownload1 () { VaR Path = server. mappath ("~ /Files/hechong tian.zip "); Return file (path, "application/X-zip-compressed "); } |
The first parameter specifies the file path, and the second parameter specifies the MIME type of the file. After you click the download link in the browser, the download window is displayed:
You should note that the file name will change to download1.zip, and the action name is used by default. We use the second overload of the file Method to Solve the file name problem:
2. Specify filedownloadname
Public actionresult filepathdownload2 () { VaR Path = server. mappath ("~ /Files/hechong tian.zip "); Return file ("G :\\ hechongtian.zip", "application/X-zip-compressed", "crane.zip "); }Public actionresult filepathdownload3 () { VaR Path = server. mappath ("~ /Files/hechong tian.zip "); VaR name = path. getfilename (PATH ); Return file (path, "application/X-zip-compressed", name ); } |
You can specify the file name by passing a value to the filedownloadname parameter. The filedownloadname does not have to be the same as the file name on the disk. The download Prompt window is as follows:
Filepathdownload2 is correct. filepathdownload3 is the action name by default. The reason is that filedownloadname is part of the URL and can only contain ASCII code. Let's improve filepathdownload3:
3. url encoding for filedownloadname
Public actionresult filepathdownload4 () {var Path = server. mappath ("~ /Files/hechong tian.zip "); var name = path. getfilename (PATH); Return file (path, "application/X-zip-compressed", URL. encode (name ));} |
Try again. The download window is as follows:
Okay, no problem. In the above Code, URL. encode (...), You can also use httputility. urlencode (...), The former calls the latter internally.
Let's take a look at filecontentresult.
Filecontentresult
Filecontentresult can directly send byte [] to the browser as a file (instead of creating a temporary file ). The reference code is as follows:
Public actionresult filecontentdownload1 () {byte [] DATA = encoding. utf8.getbytes ("Welcome to hetian's blog http://www.cnblogs.com/ldp615/"); Return file (data, "text/plain", "welcome.txt ");} |
After clicking the download link, the pop-up Prompt window is as follows:
Filestreamresult
It is not easy to find an appropriate example for filestreamresult. After all, the HTTP response already contains an output stream. If you want to dynamically generate a file, data can be directly written to the output stream, which is highly efficient. Of course, we will not directly write data to response's outputstream in the Controller. This does not comply with MVC. We should encapsulate this operation into an actionresult.
But think about the purpose. For example, if there is a compressed (or encrypted) file on the server, it needs to be decompressed (or decrypted) and sent to the user.
1. Decompress (or decrypt)
The Demo code is as follows. Decompress icsharpcode. sharpziplib. dll:
Public actionresult filestreamdownload1 () { VaR Path = server. mappath ("~ /Files/hechong tian.zip "); VaR filestream = new filestream (path, filemode. Open ); VaR zipinputstream = new zipinputstream (filestream ); VaR entry = zipinputstream. getnextentry (); Return file (zipinputstream, "application/pdf", URL. encode (entry. Name )); } |
For simplicity, it is assumed that there is only one file in the compressed file and it is in pdf format. The following figure shows the zip code of hechong tian.zip:
The following dialog box is displayed:
2. Forwarding (or leeching)
Another purpose of filestreamresult is to download files from other websites as files on this site (in fact, it is leeching ):
public ActionResult FileStreamDownload1(){ var stream = new WebClient().OpenRead("http://files.cnblogs.com/ldp615/Mvc_TextBoxFor.rar"); return File(stream, "application/x-zip-compressed", "Mvc_TextBoxFor.rar");} |
Check whether the source is localhost in the following prompt window:
The spark of thinking lights up the world!