It's better to use <a> or <iframe>
to display PDF in the html content.
And <embed>
is considered to be old-fashioned way because it is deprecated now.
So here, I have a pdf in wwwroot folder, then this is my controller:
public IActionResult Index()
{
//what we need is localhost:port/test.pdf
string filePath = Path.Combine("/test.pdf");
//below will get c://xxx/wwwroot/test.pdf
string filePath2 = Path.Combine(_hostEnvironment.WebRootPath, "test.pdf");
byte[] bytes = System.IO.File.ReadAllBytes(filePath2);
ProfileModel profile = new ProfileModel
{
url = filePath,
file = bytes
};
return View(profile);
}
And this is my view:
@model WebApp.Models.ProfileModel
<div>display PDF file content</div>
<embed src="@Model.InlineMarkup" width="100% " height="100%" type="application/pdf">
<a href='@Model.url'>show pdf file with /a/ tag</a>
<div>
<div>this is an iframe</div>
<iframe src="@Model.url" width="100%" height="500px">
</iframe>
</div>
Here's the model:
public class ProfileModel
{
public byte[] file { get; set; }
public string url { get; set; }
public string InlineMarkup
{
get
{
return String.Format("data:application/pdf;base64,{0}", Convert.ToBase64String(this.file));
}
}
}
Here's the test result
-
If the answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.
Best Regards,
TinyWang