Hi ruben,
In order to display pdf in browser you need to make use of JavaScript plugin.
Using the below article i have created the sample.
ASP.Net Core MVC: Display PDF files from Database in View
Model
public class File
{
[Key]
public int FileId { get; set; }
public string Name { get; set; }
public string Path { get; set; }
}
DBContext
public class DBCtx : DbContext
{
public DBCtx(DbContextOptions<DBCtx> options) : base(options)
{
}
public DbSet<Models.File> Files { get; set; }
}
Controller
public class HomeController : Controller
{
private DBCtx Context { get; }
private IWebHostEnvironment Environment { get; }
public HomeController(DBCtx _context, IWebHostEnvironment environment)
{
this.Context = _context;
Environment = environment;
}
public IActionResult Index()
{
List<Models.File> files = (from file in this.Context.Files
where file.Path.EndsWith(".pdf")
select file).ToList();
return View(files);
}
public IActionResult ViewFile(string path)
{
string filePath = Uri.UnescapeDataString(Path.Combine(this.Environment.WebRootPath, path)).Replace(@"\", @"/");
byte[] pdfByteArray = System.IO.File.ReadAllBytes(filePath);
string base64EncodedPDF = Convert.ToBase64String(pdfByteArray);
TempData["JavaScriptFunction"] = string.Format("ViewFile('{0}');", base64EncodedPDF);
return View();
}
}
View
Index
@using EF_Core_7_MVC.Models;
@model IEnumerable<File>
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
<table cellpadding="0" cellspacing="0">
<tr>
<th>File Id</th>
<th>Name</th>
<th>View</th>
</tr>
@foreach (File file in Model)
{
<tr>
<td>@file.FileId</td>
<td>@file.Name</td>
<td>
@if (file.Path != null)
{
@Html.ActionLink("Open", "ViewFile", "Home", new { path = file.Path }, new { target = "_blank" })
}
</td>
</tr>
}
</table>
</body>
</html>
ViewFile
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>View File</title>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/pdf.js/2.6.347/pdf.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/pdf.js/2.6.347/pdf_viewer.min.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="pdf_container"></div>
<script type="text/javascript">
function ViewFile(pdfByteArray) {
var bytes = Base64ToBytes(pdfByteArray);
LoadPdfFromBlob(bytes);
}
var pdfjsLib = window['pdfjs-dist/build/pdf'];
pdfjsLib.GlobalWorkerOptions.workerSrc = 'https://cdnjs.cloudflare.com/ajax/libs/pdf.js/2.6.347/pdf.worker.min.js';
var pdfDoc = null;
var scale = 1; //Set Scale for zooming PDF.
var resolution = 1; //Set Resolution to Adjust PDF clarity.
function LoadPdfFromBlob(blob) {
//Read PDF from BLOB.
pdfjsLib.getDocument({ data: blob }).promise.then(function (pdfDoc_) {
pdfDoc = pdfDoc_;
//Reference the Container DIV.
var pdf_container = document.getElementById("pdf_container");
pdf_container.innerHTML = "";
pdf_container.style.display = "block";
//Loop and render all pages.
for (var i = 1; i <= pdfDoc.numPages; i++) {
RenderPage(pdf_container, i);
}
});
};
function RenderPage(pdf_container, num) {
pdfDoc.getPage(num).then(function (page) {
//Create Canvas element and append to the Container DIV.
var canvas = document.createElement('canvas');
canvas.id = 'pdf-' + num;
ctx = canvas.getContext('2d');
pdf_container.appendChild(canvas);
//Create and add empty DIV to add SPACE between pages.
var spacer = document.createElement("div");
spacer.style.height = "20px";
pdf_container.appendChild(spacer);
//Set the Canvas dimensions using ViewPort and Scale.
var viewport = page.getViewport({ scale: scale });
canvas.height = resolution * viewport.height;
canvas.width = resolution * viewport.width;
//Render the PDF page.
var renderContext = {
canvasContext: ctx,
viewport: viewport,
transform: [resolution, 0, 0, resolution, 0, 0]
};
page.render(renderContext);
});
};
function Base64ToBytes(base64) {
var s = window.atob(base64);
var bytes = new Uint8Array(s.length);
for (var i = 0; i < s.length; i++) {
bytes[i] = s.charCodeAt(i);
}
return bytes;
};
</script>
@if (TempData["JavaScriptFunction"] != null)
{
<script type="text/javascript">
@Html.Raw(TempData["JavaScriptFunction"])
</script>
}
</body>
</html>
Screenshot