Render Or Display A Document From MemoryStream In Razor View

Introduction 

 
You are working on a web application and the business need is to display all kinds of file formats (e.g. Word, Excel, PDF, Presentation, MSG, EML, PSD, CAD) to the client. The restriction or limitation is not to install native applications on the server. This is because installing MS Office, Autodesk, Adobe Acrobat will surely require a lot of resources (e.g. space and RAM). Let's discuss a one-stop solution for this requirement.
 

Proposed Solution

 
What about a stand-alone API (.NET Core compatible) that takes any supported file format as input, renders it to PDF, HTML, or Image file format (depending on the use-case). Later, these resultant files could be displayed in the browser or any other application (e.g. Windows Forms, Mobile). 
 
Key Features 
  • Extract source document information (e.g. file type, page count)
  • Document transformation (for example you can add textual watermark, page rotation, and reordering)
  • Increase rendering performance by caching rendering results
  • Load document from the local driver or from cloud storage (e.g. FTP, Azure Blob, Amazon S3, URL, Stream)
  • Specify document encoding while rendering
  • Load a password-protected file 

Implementation

 
In your HomeController/Index, you will get the source file. Convert it to byte[] and pass byte[] to MemoryStream. After that, you will pass the MemoryStream to the Razor View using ViewData.
  1. public class HomeController : Controller  
  2. {  
  3.       private readonly ILogger<HomeController> _logger;  
  4.       public HomeController(ILogger<HomeController> logger)  
  5.       {  
  6.           _logger = logger;  
  7.       }  
  8.       public IActionResult Index()  
  9.       {  
  10.           string pageContent = $"This content is passed through ViewData.";  
  11.           byte[] pageBytes = Encoding.UTF8.GetBytes(pageContent);  
  12.           MemoryStream outputStream = new MemoryStream(pageBytes);  
  13.   
  14.           ViewData["PageContent"] = outputStream;  
  15.   
  16.           return View();  
  17.       }  
  18.       public IActionResult Page(int pageNumber)  
  19.       {  
  20.           string pageContent = $"This is page {pageNumber}.";  
  21.           byte[] pageBytes = Encoding.UTF8.GetBytes(pageContent);  
  22.           MemoryStream outputStream = new MemoryStream(pageBytes);  
  23.   
  24.           return File(outputStream, "text/html");  
  25.       }  
  26.       [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]  
  27.       public IActionResult Error()  
  28.       {  
  29.           return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });  
  30.       }  
  31. }  
In View, you have to Encode (UTF8) the stream and display it using Html.Raw. Have a look at the output (I tried to display a Word document).
 
Render Or Display A Document From MemoryStream In Razor View
 

Conclusion

 
Using this standard web application, you can display any (top and mostly used) file formats in the browser without third party tool or software dependency. The best thing about this API is that it's UI-Independent. Hence, you can develop customized tool-bars or even page previews (thumbnails) in your application. You can post here, in case of any API related issue(s).
Next Recommended Reading Partial View in ASP.NET