0

I am trying to show the document located inside wwwroot folder in my .NET MVC Core application folder in an iframe.

Scenario:

I have a list of document on my Razor page: MyRazor.cshtml and there is a button for each list. When I click that button it takes me to the details of individual document using its Id.

When the page loads, the document gets downloaded instead of showing in iframe src.

Controller:

public ActionResult GetArticles(int id)
    {
        try
        {
            Articles getArticles = _articleRepository.GetArticles(id);
            return View(getArticles);
        }
        catch (Exception)
        {

            return NotFound();
        }
    }

View:

<div class="card">
    <iframe id="documentViewer" src="~/docs/@Html.DisplayFor(model => model.WordDocumentFile)" width="100%" height="700px;" frameborder="0"></iframe>
</div>

which is undesirable. I just want the show that document inside my iframe.

What am I missing?

Rohan Rao
  • 2,505
  • 3
  • 19
  • 39

2 Answers2

2

Because doc files are not known by the web browsers, you need to transform them to HTML or PDF.

The best way is to store the PDF file in the corresponding folder, and then read the PDF file directly to display the content of the document, rather than download the file.

You can also refer to this:How do I render a Word document (.doc, .docx) in the browser using JavaScript?

LouraQ
  • 6,443
  • 2
  • 6
  • 16
0

Yongqing Yu's answer sort of stated it, but I just want to elaborate that if the PDF is in your wwwroot folder, all you need to do is

<iframe src="~/docs/mypdf.pdf">

On a view, ~/ is already relative to the static files stored in wwwroot

Ben Sampica
  • 2,912
  • 1
  • 20
  • 25