Display A Document From SQL Server To A Web Page In ASP.NET MVC

Display A Document From SQL Server To A Web Page In ASP.NET MVC

In this blog, you will learn how to display a document from SQL Server to a Web Page in ASP.NET MVC.

Introduction 

In this blog post, I'll discuss a useful yet easy to implement document viewer API. Using that you can display a source file (e.g. Word, Presentation, Diagram, PSD) from your server to your browser without downloading it.

Some Use-Cases

  • Display a Word resume to a user in a web browser
  • Render a Visio Diagram on the web page
  • View a Presentation or a Slide online without downloading it

*Implementation *

Now as the use-cases are clear, we will dive into the API implementation. It'll be an ASP.NET MVC application. We'll pull data/documents from the database and save it to a stream. _You have to specify the file format in order to render it correctly. _The source document will be then rendered to HTML. Eventually, our controller will return this stream to the View/browser.  

public ActionResult Index()  
{  
   License lic = new License();  
   lic.SetLicense(@"D:/GD Licenses/Conholdate.Total.NET.lic");  
   MemoryStream outputStream = new MemoryStream();  
   //specify just the file name if you are pulling the data from database  
   string fileName = "sample.pdf";  

   FileType fileType = FileType.FromExtension(Path.GetExtension(fileName));  

     using (Viewer viewer = new Viewer(() => GetSourceFileStream(fileName), () => new LoadOptions(fileType)))  
   {  
      HtmlViewOptions Options = HtmlViewOptions.ForEmbeddedResources(  
        (pageNumber) => outputStream,  
        (pageNumber, pageStream) => { });  
      viewer.View(Options);  
   } 
   outputStream.Position = 0;  
    return File(outputStream, "text/html");  

}  
private Stream GetSourceFileStream(string fileName) =>  
            new MemoryStream(GetSourceFileBytesFromDb(fileName));  

//TODO: If you want to pull the data from the DB  
private byte[] GetSourceFileBytesFromDb(string fileName) =>  
            System.IO.File.ReadAllBytes(fileName);  

Have a look at this image/screenshot. We displayed a PDF from Server to Browser.

asp.net

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

jQuery Ajax CRUD in ASP.NET Core MVC with Modal Popup

In this article, we’ll discuss how to use jQuery Ajax for ASP.NET Core MVC CRUD Operations using Bootstrap Modal. With jQuery Ajax, we can make HTTP request to controller action methods without reloading the entire page, like a single page application.

Hire ASP.Net Developers

Looking to outsource your asp dot net development requirement? ASP.Net is a special feature of the DOT Net framework created by Microsoft. At [HourlyDeveloper.io](https://hourlydeveloper.io/ "HourlyDeveloper.io"), we have a team of experienced...

MVC User Registration & Login with ASP.NET Core Identity

In this article, we will discuss the quickest way to use ASP.NET Core Identity for User Login and Registration in a new or existing MVC application.

Running WordPress on ASP.NET Core with Peachpie

In this article, you will learn how to use or integrate WordPress in ASP.NET and Running WordPress on ASP.NET Core, without PHP, or any source files on the server. The following demonstration will show you how to add WordPress as a frontend to an existing ASP.NET Core application step by step.

Routing in MVC - ASP.NET Core Demystified

ASP.NET Core MVC has introduced quite a few concepts that new (or new-to-ASP.NET) web developers might have some difficulty getting caught up with. My ASP.NET Core Demystified series is designed to help these developers get started building their own custom, full-fledged, working AASP.NET Core applications. In