Click here to Skip to main content
15,891,204 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,

I have .pdf file and need to view in brower in MVC5
When i am not able to do

What I have tried:

<div id="divReport" style="width:100%;text-align:center; height:100% overflow:hidden;">
    <div id='PDF123'>
       @{Url.Action("DisplayPDF", "PICSFS010");}
   
    </div>

</div>
Controller Action :

public FileResult DisplayPDF()
        {
          return File("~/HelpFile/awstats.pdf", "application/pdf");
        }

<script>
<pre><script type="text/javascript">
  function Initialize_view() {
        showloader();
        $.ajax({
            url: "/PICSFS010/DisplayPDF",
            cache: false,
            dataType: "html",
            success: function (data) {
                debugger;
                $("#PDF123").html(data);
            },
            error: function (data) {
                debugger;
                hideloader();
            }
        });
    }
<script>
Posted
Updated 21-Jun-17 0:02am
Comments
F-ES Sitecore 21-Jun-17 5:54am    
Just redirect to the browser to the location of the PDF file and let it handle the display

1 solution

What are you tried here is put whatever File("~/HelpFile/awstats.pdf", "application/pdf") returns (the content of the pdf?) inside the #PDF123 element as HTML content... That will not work anyway, as the browser will try to interpret it as HTML (after all you told it to do so) and it will fail of course...
The most simple way to show the user a file is to link to it (redirect the browser to it), however even then you can not be sure where (and by what application) the file will be opened... That's depends on the end user's favorites, so it can end with an in-bowser display, with a 3rd party application opening or with simple download...

In case you do not want to reveal the location of the file, this article try to explain how you can do that: Stream Your Documents Using Simple Web Page[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900