embedding a pdf file in my chrome browser

Anjali Agarwal 1,531 Reputation points
2023-02-03T04:57:40.42+00:00

I am trying to embed a pdf file on my web page. In order to achieve this,

I have the following code in my controller:

public IActionResult Index()

    {



        string path = Path.Combine(_environment.WebRootPath, "packageFiles");

        using (MemoryStream stream = new MemoryStream())

        {

            System.IO.File.WriteAllBytes(path + "/Birth.pdf", stream.ToArray());

            string embed = "<object data=\"{0}\" type=\"application/pdf\" width=\"800px\" height=\"800px\">";

            embed += "If you are unable to view file, you can download from <a href = \"{0}\">here</a>";

            embed += " or download <a target = \"_blank\" href = \"http://get.adobe.com/reader/\">Adobe PDF Reader</a> to view the file.";

            embed += "</object>";

            TempData["Embed"] = string.Format(embed, "/packageFiles/Birth.pdf");

            return View();

          

        }

    }

and following code on my view

	@Html.Raw(TempData["Embed"])
This is not showing pdf file on my web page. I also checked the chrome browser settings 

	and it is set to display pdf files. Alternatively, I also tried the code below 

	to display the pdf file on my controller

string embed = "<object class="ss-pdfjs-viewer" data="{0}" type="application/pdf" allowfullscreen webkitallowfullscreen>";

        embed += "</object>";

        

       TempData["Embed"] =  string.Format(embed, ResolveUrl("~/packageFiles/Birth.pdf"));

	

This doesnt work either. I want to display a pdf file embedded in my browser. Below is my file structure:

User's image

Any help with this will be greatly appreciated.

Developer technologies | ASP.NET | ASP.NET Core
Developer technologies | ASP.NET | Other
0 comments No comments
{count} votes

Accepted answer
  1. Anonymous
    2023-02-03T08:59:38.29+00:00

    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

    User's image

    -

    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

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.