Understand the nature of several actionresult of asp.net mvc: Fileresult

Source: Internet
Author: User
Tags abstract constructor header httpcontext

Fileresult is a file-based actionresult, using Fileresult we can easily respond to the contents of a physical file to the client. asp.net mvc defines three specific fileresult, namely Filecontentresult, Filepathresult, and Filestreamresult. In this article we will explore three specific fileresult how to respond to a request with a file's contents.

First, Fileresult

As shown in the following code snippet, Fileresult has a read-only property contenttype that represents the media type, which is initialized in the constructor. When we create the corresponding Fileresult object based on a physical file, we should specify the media type according to the type of file, for example, the target file is a. jpg picture, then the corresponding media type is "Image/jpeg", and for a. pdf file, use the " Application/pdf ".

   1:public Abstract class Fileresult:actionresult
2: {
3: protected Fileresult (string contentType);
4: Public override void Executeresult (ControllerContext context);
5: protected abstract void WriteFile (httpresponsebase response);
6:
7: Public string ContentType {get;}
8: Public string Filedownloadname {get; set;}
9:}

The response to a file has two forms, namely, inline (Inline) and attachments (attachment). In general, the former will use the browser to open the response file directly, the latter will be downloaded to the client in a separate file. For the latter, we typically specify a filename for the downloaded file, which can be specified by the Fileresult Filedownloadname property. File responses are inline by default, and if you need to take the form of an attachment, you need to create a header named Content-disposition for the response, which is formatted as "attachment;" filename={Filedownloadname} ".

Fileresult is just an abstract class, and the output of the file content is implemented in an abstract method WriteFile, which is invoked in the overridden Executeresult method. If the Filedownloadname property is not empty, it means that the file response will be in the form of an attachment, fileresult the setting of the Content-disposition response header in the overridden Executeresult method. The following code fragment basically embodies the implementation of the Executeresult method in Fileresult.

   1:public Abstract class Fileresult:actionresult
2: {
3: //other Members
4: Public override void Executeresult (ControllerContext context)
5: {
6: httpresponsebase response = context. Httpcontext.response;
7: Response. ContentType = this. ContentType;
8: if (!string. IsNullOrEmpty (this. Filedownloadname))
9: {
: //Generate Content-disposition response header value
One: string headervalue = Contentdispositionutil.getheadervalue (this. Filedownloadname);
The context . HttpContext.Response.AddHeader ("Content-disposition", Headervalue);
: }
A: this . WriteFile (response);
: }
16:}

asp.net mvc defines three specific fileresult, respectively, Filecontentresult, Filepathresult, and Filestreamresult, and then we'll introduce them separately.

Second, Filecontentresult

The

Filecontentresult is a fileresult created for the contents of a file. As shown in the following code fragment, Filecontentresult has a read-only property with a byte array type filecontents represents the contents of the response file, which is specified in the constructor. The Filecontentresult response to the file content is also simple, as can be seen from the WriteFile method definition shown below, It simply invokes the write method of the current HttpResponse OutputStream property to write the byte array representing the contents of the file directly to the response output stream.

   1:public class Filecontentresult:fileresult
2: {
3: Public byte[] filecontents {get;}
4: Public filecontentresult (byte[] filecontents, string contentType);
5:
6: protected override void WriteFile (httpresponsebase response)
7: {
8: Response. Outputstream.write (this. FileContents, 0, this. Filecontents.length);
9: }
10:}
11:
12:public abstract class Controller:controllerbase, ...
13: {
: //other Members
: protected Filecontentresult File (byte[] filecontents, string contentType);
: protected Virtual Filecontentresult File (byte[] filecontents, String contentType, string filedownloadname);
17:}

The abstract class controller defines the following two file overloads to generate the corresponding filecontentresult based on the specified byte array, media type, and download file name (optional). Because Filecontentresult is created from a byte array, Filecontentresult is a good choice when we need to dynamically generate the content of the response file instead of reading it from a physical file.

Related Article

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.