在 Controller 中我们可以使用 FileResult 向客户端发送文件。
FileResult
FileResult 是一个抽象类,继承自 ActionResult。在 System.Web.Mvc.dll 中,它有如上三个子类,分别以不同的方式向客户端发送文件。
在实际使用中我们通常不需要直接实例化一个 FileResult 的子类,因为 Controller 类已经提供了六个 File 方法来简化我们的操作:
protected internal FilePathResult File(string fileName, string contentType); protected internal virtual FilePathResult File(string fileName, string contentType, string fileDownloadName); protected internal FileContentResult File(byte[] fileContents, string contentType); protected internal virtual FileContentResult File(byte[] fileContents, string contentType, string fileDownloadName); protected internal FileStreamResult File(Stream fileStream, string contentType); protected internal virtual FileStreamResult File(Stream fileStream, string contentType, string fileDownloadName);
FilePathResult
FilePathResult 直接将磁盘上的文件发送至浏览器: