mvc 下载示例(from All-In-One Code Framework)
本例实现了浏览服务器端文件和选中文件列表中某一文件进行下载。
FileController:
代码
public class FileController : Controller
{
// Action for list all the files in "~/App_Data/download" directory
public ActionResult List()
{
// Retrive file list
DirectoryInfo dir = new DirectoryInfo(Server.MapPath("~/App_Data/download/"));
// Filter it via LINQ to Object
var files = from f in dir.GetFiles("*.*", SearchOption.TopDirectoryOnly)
where f.Extension != "exe"
select f;
// Call the corresponding View
return View(files.ToList());
}
// Action for return the binary stream of a specified file
public ActionResult Download(string fn)
{
// Check whether requested file is valid
string pfn = Server.MapPath("~/App_Data/download/" + fn);
if (!System.IO.File.Exists(pfn))
{
throw new ArgumentException("Invalid file name or file not exists!");
}
// Use BinaryContentResult to encapsulate the filecontent and return it
return new BinaryContentResult()
{
FileName = fn,
ContentType = "application/octet-stream",
Content = System.IO.File.ReadAllBytes(pfn)
};
}
}
{
// Action for list all the files in "~/App_Data/download" directory
public ActionResult List()
{
// Retrive file list
DirectoryInfo dir = new DirectoryInfo(Server.MapPath("~/App_Data/download/"));
// Filter it via LINQ to Object
var files = from f in dir.GetFiles("*.*", SearchOption.TopDirectoryOnly)
where f.Extension != "exe"
select f;
// Call the corresponding View
return View(files.ToList());
}
// Action for return the binary stream of a specified file
public ActionResult Download(string fn)
{
// Check whether requested file is valid
string pfn = Server.MapPath("~/App_Data/download/" + fn);
if (!System.IO.File.Exists(pfn))
{
throw new ArgumentException("Invalid file name or file not exists!");
}
// Use BinaryContentResult to encapsulate the filecontent and return it
return new BinaryContentResult()
{
FileName = fn,
ContentType = "application/octet-stream",
Content = System.IO.File.ReadAllBytes(pfn)
};
}
}
BinaryContentResult 类定义:
代码
public class BinaryContentResult : ActionResult
{
// Default constructor
public BinaryContentResult()
{
}
// Properties for encapsulating http headers
public string ContentType { get; set; }
public string FileName { get; set; }
public byte[] Content { get; set; }
// Underlying code which set the httpheaders and output file content
public override void ExecuteResult(ControllerContext context)
{
context.HttpContext.Response.ClearContent();
context.HttpContext.Response.ContentType = ContentType;
context.HttpContext.Response.AddHeader("content-disposition",
"attachment; filename=" + FileName);
context.HttpContext.Response.BinaryWrite(Content);
context.HttpContext.Response.End();
}
}
{
// Default constructor
public BinaryContentResult()
{
}
// Properties for encapsulating http headers
public string ContentType { get; set; }
public string FileName { get; set; }
public byte[] Content { get; set; }
// Underlying code which set the httpheaders and output file content
public override void ExecuteResult(ControllerContext context)
{
context.HttpContext.Response.ClearContent();
context.HttpContext.Response.ContentType = ContentType;
context.HttpContext.Response.AddHeader("content-disposition",
"attachment; filename=" + FileName);
context.HttpContext.Response.BinaryWrite(Content);
context.HttpContext.Response.End();
}
}
由上面的定义我们可以看到,我们可以重载不同的ActionResult。ActionResult类定义如下:
代码
namespace System.Web.Mvc
{
// 摘要:
// Encapsulates the result of an action method and is used to perform a framework-level
// operation on the action method's behalf.
public abstract class ActionResult
{
protected ActionResult();
// 摘要:
// Enables processing of the result of an action method by a custom type that
// inherits from System.Web.Mvc.ActionResult.
//
// 参数:
// context:
// The context within which the result is executed.
public abstract void ExecuteResult(ControllerContext context);
}
}
{
// 摘要:
// Encapsulates the result of an action method and is used to perform a framework-level
// operation on the action method's behalf.
public abstract class ActionResult
{
protected ActionResult();
// 摘要:
// Enables processing of the result of an action method by a custom type that
// inherits from System.Web.Mvc.ActionResult.
//
// 参数:
// context:
// The context within which the result is executed.
public abstract void ExecuteResult(ControllerContext context);
}
}
我感觉这个示例比较好,思路很清晰。此示例对我感觉最深的是提供一个mvc下载文件的解决方案,还有我们可以自己定义一个能满足自己需要的ActionResult类。