1 /* ****************************************************************************
\r
3 * Copyright (c) Microsoft Corporation. All rights reserved.
\r
5 * This software is subject to the Microsoft Public License (Ms-PL).
\r
6 * A copy of the license can be found in the license.htm file included
\r
7 * in this distribution.
\r
9 * You must not remove this notice, or any other, from this software.
\r
11 * ***************************************************************************/
\r
13 namespace System.Web.Mvc {
\r
15 using System.Net.Mime;
\r
17 using System.Web.Mvc.Resources;
\r
19 public abstract class FileResult : ActionResult {
\r
21 protected FileResult(string contentType) {
\r
22 if (String.IsNullOrEmpty(contentType)) {
\r
23 throw new ArgumentException(MvcResources.Common_NullOrEmpty, "contentType");
\r
26 ContentType = contentType;
\r
29 private string _fileDownloadName;
\r
31 public string ContentType {
\r
36 public string FileDownloadName {
\r
38 return _fileDownloadName ?? String.Empty;
\r
41 _fileDownloadName = value;
\r
45 public override void ExecuteResult(ControllerContext context) {
\r
46 if (context == null) {
\r
47 throw new ArgumentNullException("context");
\r
50 HttpResponseBase response = context.HttpContext.Response;
\r
51 response.ContentType = ContentType;
\r
53 if (!String.IsNullOrEmpty(FileDownloadName)) {
\r
54 // From RFC 2183, Sec. 2.3:
\r
55 // The sender may want to suggest a filename to be used if the entity is
\r
56 // detached and stored in a separate file. If the receiving MUA writes
\r
57 // the entity to a file, the suggested filename should be used as a
\r
58 // basis for the actual filename, where possible.
\r
59 ContentDisposition disposition = new ContentDisposition() { FileName = FileDownloadName };
\r
60 string headerValue = disposition.ToString();
\r
61 context.HttpContext.Response.AddHeader("Content-Disposition", headerValue);
\r
64 WriteFile(response);
\r
67 protected abstract void WriteFile(HttpResponseBase response);
\r