asp: Send files to the browser via Fileresult

Source: Internet
Author: User
Tags decrypt

In the Controller we can use Fileresult to send files to the client.

Fileresult

Fileresult is an abstract class that inherits from ActionResult. In System.Web.Mvc.dll, it is like the last three subclasses, which send files to the client in different ways.

In practice we do not usually need to instantiate a Fileresult subclass directly, because the Controller class has provided six File methods to simplify our operation:

protected InternalFilepathresult File (stringFileName,stringContentType);protected Internal VirtualFilepathresult File (stringFileName,stringContentType,stringFiledownloadname);protected InternalFilecontentresult File (byte[] filecontents,stringContentType);protected Internal VirtualFilecontentresult File (byte[] filecontents,stringContentType,stringFiledownloadname);protected InternalFilestreamresult File (Stream fileStream,stringContentType);protected Internal VirtualFilestreamresult File (Stream fileStream,stringContentType,stringFiledownloadname);
Filepathresult

Filepathresult directly sends the file on the disk to the browser:

1. The simplest way
 Public ActionResult FilePathDownload1 () {    var path = Server.MapPath ("~/files/crane cupola. zip");    return File (Path, "application/x-zip-compressed");}

The first parameter specifies a file path, and the second parameter specifies the MIME type of the file.

When the user clicks the download link on the browser, it will bring up the download window:

It should be noted that the file name becomes Download1.zip, which is the name of the Action 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/crane cupola. zip "); return  File ("g:\\ crane cupola. zip ", " application/x-zip-compressed  "," crane.zip  ");} public  ActionResult FilePathDownload3 () { var  path = Server.MapPath ("~/files/crane cupola. zip  ");    var  name = Path.getfilename (Path); return  File (Path, "application/x-zip-compressed ", name);} 

We can specify the file name by passing a value to the Filedownloadname parameter, Filedownloadname does not have to be the same as the file name on the disk. The download prompt window is as follows:

FilePathDownload2 no problem, FILEPATHDOWNLOAD3 is still the default name for Action. The reason is that filedownloadname will be part of the URL and can contain only ASCII code. Let's make FilePathDownload3 a little bit better:

3. URL Encoding for Filedownloadname
 Public ActionResult FilePathDownload4 () {    var path = Server.MapPath ("~/files/crane cupola. 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. The Url.encode (...) in the code above can also use the Httputility.urlencode (...), which calls the latter internally.

We'll see Filecontentresult again.

Filecontentresult

Filecontentresult can send byte[] as a file directly to the browser (without creating a temporary file). The reference code is as follows:

 Public ActionResult FileContentDownload1 () {    byte[] data = Encoding.UTF8.GetBytes (" Welcome to the Crane's blog http.// www.cnblogs.com/ldp615/");    return File (data, "text/plain", "welcome.txt");}

After clicking the download link, the following popup window is displayed:

Filestreamresult

It is not easy to find a suitable example for Filestreamresult, after all, the Http Response already contains an output stream, if you want to generate files dynamically, you can write data directly to the output stream, the efficiency is high. Of course, we do not write data directly to Response's outputstream in the Controller, which is incompatible with MVC, and we should encapsulate this action as a actionresult.

But think about it, the use of some, such as a compressed (or encrypted) file on the server, need to extract (or decrypt) sent to the user.

1. Unzip (or decrypt)

The demo code is as follows, decompression using ICSharpCode.SharpZipLib.dll:

 Public ActionResult FileStreamDownload1 () {    var path = Server.MapPath ("~/files/crane cupola. zip");    var New FileStream (path, filemode.open);    var New Zipinputstream (fileStream);    var entry = Zipinputstream.getnextentry ();    return File (Zipinputstream, "application/pdf", Url.encode (entry. Name));}

For simplicity, assume that there is only one file in the compressed file, and that it is in PDF format. The crane soars. zip as follows:

Click to pop up the download prompt window as follows:

2. Forwarding (or hotlinking)

Another use of Filestreamresult is to download the files on other websites as the site files (in fact, hotlinking):

 Public ActionResult FileStreamDownload1 () {    new WebClient (). OpenRead ("http://files.cnblogs.com/ldp615/Mvc_TextBoxFor.rar");    return File (Stream, "application/x-zip-compressed", "mvc_textboxfor.rar");}

Look at the prompt window below, source or localhost:

asp: Send files to the browser via Fileresult

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.