- mvc_chart1.zip
- Mvc_Chart
- _svn
- dir-prop-base
- entries
- format
- prop-base
- props
- text-base
- Default.aspx.cs.svn-base
- Default.aspx.svn-base
- Global.asax.cs.svn-base
- Global.asax.svn-base
- Mvc_Chart.csproj.svn-base
- Web.config.svn-base
- tmp
- prop-base
- props
- text-base
- App_Data
- _svn
- entries
- format
- prop-base
- props
- text-base
- tmp
- prop-base
- props
- text-base
- bin
- Mvc_Chart.dll
- Content
- _svn
- entries
- format
- prop-base
- props
- text-base
- Site.css.svn-base
- tmp
- prop-base
- props
- text-base
- Site.css
- Controllers
- Default.aspx
- Default.aspx.cs
- Global.asax
- Global.asax.cs
- Models
- _svn
- entries
- format
- prop-base
- props
- text-base
- tmp
- prop-base
- props
- text-base
- StaticModel.cs
- Mvc_Chart.csproj
- Mvc_Chart.csproj.user
- Properties
- _svn
- entries
- format
- prop-base
- props
- text-base
- AssemblyInfo.cs.svn-base
- tmp
- prop-base
- props
- text-base
- AssemblyInfo.cs
- Scripts
- Views
- _svn
- entries
- format
- prop-base
- props
- text-base
- Web.config.svn-base
- tmp
- prop-base
- props
- text-base
- Account
- Home
- Shared
- Web.config
- Web.config
- Mvc_Chart.zip
|
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Mime;
using System.Text;
using System.Web;
using System.Web.Mvc;
namespace Mvc_Chart.Controllers
{
public class FileResult : ActionResult
{
#region Constructors
public FileResult(string fileName, string contentType, IEnumerable<byte> data)
: this(fileName, contentType, data, null)
{
}
public FileResult(string fileName, string contentType, IEnumerable<byte> data, IDictionary<string, string> headers)
{
this.FileName = fileName;
this.ContentType = contentType;
this.Data = data;
this.Headers = headers;
}
public FileResult(string fileName, string contentType, Encoding contentEncoding, IEnumerable<byte> data)
: this(fileName, contentType, contentEncoding, data, null)
{
}
public FileResult(string fileName, string contentType, Encoding contentEncoding, IEnumerable<byte> data, IDictionary<string, string> headers)
: this(fileName, contentType, data, headers)
{
this.ContentEncoding = contentEncoding;
}
public FileResult(string fileName, string contentType, string path)
: this(fileName, contentType, path, null)
{
}
public FileResult(string fileName, string contentType, string path, IDictionary<string, string> headers)
{
this.FileName = fileName;
this.ContentType = contentType;
this.Data = ReadBytes(path);
this.Headers = headers;
}
#endregion
IEnumerable<byte> ReadBytes(string path)
{
var stream = File.OpenRead(path);
var bytes = new byte[1024];
int n;
while ((n = stream.Read(bytes, 0, bytes.Length)) != 0)
{
for (int i = 0; i < n; i++)
{
yield return bytes[i];
}
}
}
// Methods
public override void ExecuteResult(ControllerContext context)
{
if (context == null)
{
throw new ArgumentNullException("context");
}
HttpResponseBase response = context.HttpContext.Response;
response.Clear();
response.ClearContent();
if (!string.IsNullOrEmpty(this.ContentType))
{
response.ContentType = this.ContentType;
}
else
{
response.ContentType = MediaTypeNames.Text.Plain;
}
response.ContentEncoding = this.ContentEncoding ?? Encoding.Default;
if (!String.IsNullOrEmpty(this.FileName))
{
response.AppendHeader("content-disposition", "attachment; filename=" + this.FileName);
}
if (this.Headers != null)
{
foreach (var header in Headers)
{
response.AppendHeader(header.Key, header.Value);
}
}
if (this.Data != null)
{
response.BinaryWrite(this.Data.ToArray());
}
}
// Properties
public IEnumerable<byte> Data { get; set; }
public string FileName { get; set; }
public Encoding ContentEncoding { get; set; }
public string ContentType { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public IDictionary<string, string> Headers { get; set; }
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
Contractor in Desktop and Web applications.
Gold Coast, Queensland.