FileResult In ASP.NET Core MVC

原文:https://www.c-sharpcorner.com/article/fileresult-in-asp-net-core-mvc2/

 

This article is an overview of FileResult in ASP.Net Core MVC. The FileResult actions are used to read and write files. FileResult is the parent of all file-related action results. There is a method on ControllerBase class called File. This method accepts a set of parameters based on the type of file and its location, which maps directly to the more specific return types. I’ll discuss how to use all the FileResult actions available in ASP.Net Core MVC. Here is code.
There are different type of file results in core MVC.
  1. FileResult
  2. FileContentResult
  3. FileStreamResult
  4. VirtualFileResult
  5. PhysicalFileResult
Action Method
Description
FileResult
Represents an ActionResult that when executed will write a file as the response.
FileContentResult
Represents an ActionResult that when executed will write a binary file to the response.
FileStreamResult
Represents an ActionResult that when executed will write a file from a stream to the response.
PhysicalFileResult
A FileResult on execution will write a file from disk to the response using mechanisms provided by the host.
VirtualFileResult
A FileResult that on execution writes the file specified using a virtual path to the response using mechanisms provided by the host.
 
FileResult
 
FileResult is the parent of all file-related action results. It is a base class that is used to send binary file content to the response. It represents an ActionResult that when executed will write a file as the response.
 
  1. public FileResult DownloadFile()  
  2. {  
  3.      return File("/Files/File Result.pdf", "text/plain", "File Result.pdf");  
  4. }  
FileContentResult
 
FileContentResult is an ActionResult that when executed will write a binary file to the response.
 
  1. public FileContentResult DownloadContent()  
  2. {  
  3.             var myfile = System.IO.File.ReadAllBytes("wwwroot/Files/FileContentResult.pdf");  
  4.             return new FileContentResult(myfile, "application/pdf");  
  5. }  
FileStreamResult
 
FileStreamResult Sends binary content to the response by using a Stream instance when we want to return the file as a FileStream.
 
  1. public FileStreamResult CreateFile()  
  2. {  
  3.    var stream = new MemoryStream(Encoding.ASCII.GetBytes("Hello World"));  
  4.    return new FileStreamResult(stream, new MediaTypeHeaderValue("text/plain"))  
  5.    {  
  6.       FileDownloadName = "test.txt"  
  7.    };  
  8. }  
VirtualFileResult
 
A FileResult that on execution writes the file specified using a virtual path to the response using mechanisms provided by the host. You can use VirtualFileResult if you want to read a file from a virtual address and return it.
 
  1. public VirtualFileResult VirtualFileResult()  
  2. {  
  3.     return new VirtualFileResult("/Files/PhysicalFileResult.pdf", "application/pdf");  
  4. }  
PhysicalFileResult
 
A FileResult on execution will write a file from disk to the response using mechanisms provided by the host.You can use PhysicalFileResult to read a file from a physical address and return it, as shown in PhysicalFileResult method.
 
  1. public PhysicalFileResult PhysicalFileResult()  
  2. {  
  3.    return new PhysicalFileResult(_environment.ContentRootPath + "/wwwroot/Files/PhysicalFileResult.pdf", "application/pdf");  
  4. }  
Step 1
 
Open Visual Studio 2019 and select the ASP.NET Core Web Application template and click Next.
 
Step 2
 
Name the project FileResultActionsCoreMvc_Demo and click Create.
 
Step 3
 
Select Web Application (Model-View-Controller), and then select Create. Visual Studio used the default template for the MVC project you just created.
 
Step 4
 
In Solution Explorer, right-click the wwwroot folder. Select Add > New Folder. Name the folder Files. Add some files to work with them.
 
Complete controller code
 
 
  1. using Microsoft.AspNetCore.Hosting;  
  2. using Microsoft.AspNetCore.Mvc;  
  3. using Microsoft.Net.Http.Headers;  
  4. using System.IO;  
  5. using System.Text;  
  6.   
  7. namespace FileResultActionsCoreMvc_Demo.Controllers  
  8. {  
  9.     public class HomeController : Controller  
  10.     {  
  11.         private readonly IWebHostEnvironment _environment;  
  12.   
  13.         public HomeController(IWebHostEnvironment environment)  
  14.         {  
  15.             _environment = environment;  
  16.         }  
  17.         public IActionResult Index()  
  18.         {  
  19.             return View();  
  20.         }  
  21.   
  22.         public FileResult DownloadFile()  
  23.         {  
  24.             return File("/Files/File Result.pdf", "text/plain", "File Result.pdf");  
  25.         }  
  26.   
  27.         public FileContentResult DownloadContent()  
  28.         {  
  29.             var myfile = System.IO.File.ReadAllBytes("wwwroot/Files/FileContentResult.pdf");  
  30.             return new FileContentResult(myfile, "application/pdf");  
  31.         }  
  32.   
  33.         public FileStreamResult CreateFile()  
  34.         {  
  35.             var stream = new MemoryStream(Encoding.ASCII.GetBytes("Hello World"));  
  36.             return new FileStreamResult(stream, new MediaTypeHeaderValue("text/plain"))  
  37.             {  
  38.                 FileDownloadName = "test.txt"  
  39.             };  
  40.         }  
  41.         public VirtualFileResult VirtualFileResult()  
  42.         {  
  43.             return new VirtualFileResult("/Files/PhysicalFileResult.pdf", "application/pdf");  
  44.         }  
  45.   
  46.         public PhysicalFileResult PhysicalFileResult()  
  47.         {  
  48.             return new PhysicalFileResult(_environment.ContentRootPath + "/wwwroot/Files/PhysicalFileResult.pdf", "application/pdf");  
  49.         }  
  50.   
  51.     }  
  52. }  
Step 5
 
Open Index view which is in views folder under Home folder. Add the below code in Index view.
 
Index View
 
  1. @{  
  2.     ViewData["Title"] = "Home Page";  
  3. }  
  4.   
  5. <h3 class="text-center text-uppercase">FileResult Action in core mvc</h3>  
  6. <ul class="list-group list-group-horizontal">  
  7.     <li class="list-group-item"><a asp-action="DownloadFile" asp-controller="Home">File Result</a></li>  
  8.     <li class="list-group-item"><a asp-action="DownloadContent" asp-controller="Home" target="_blank">File Content Result</a></li>  
  9.     <li class="list-group-item"><a asp-action="CreateFile" asp-controller="Home">File Stream Result</a></li>  
  10.     <li class="list-group-item"><a asp-action="VirtualFileResult" asp-controller="Home" target="_blank">Virtual File Result</a></li>  
  11.     <li class="list-group-item"><a asp-action="PhysicalFileResult" asp-controller="Home" target="_blank">Physical File Result</a></li>  
  12. </ul>  
Step 6
 
Build and run your Project ctrl+F5
posted @ 2020-12-03 20:35  PanPan003  阅读(1340)  评论(0编辑  收藏  举报