简体   繁体   中英

Open a PDF file in a new tab in .NET MVC

I have a button that generates a PDF file. I am trying to have the PDF file open in a new window but so far I have been unsuccessful.

Button code:

<button type="button" class="btn btn-warning" onclick="location.href = '@Url.Action("/DetailToPdf/"+ comb.Id, "Combined")'"
                        data-placement="top" data-container="body" data-toggle="popover" data-content="PDF" data-trigger="hover">
                    <span class="glyphicon glyphicon-download-alt" aria-hidden="true"></span>
                </button>

Relevant code of my controller:

public ActionResult DetailToPdf(long id = 0)
    {
        ...

        //Convert HTML string to PDF
        PdfDocument document = htmlConverter.Convert(htmlText, baseUrl);
        
        MemoryStream stream = new MemoryStream();

        //Save and close the PDF document 
        document.Save(stream);

        document.Close(true);

        return File(stream.ToArray(), "application/pdf", model.Id + "_approval.pdf");
    }

Things that I have tried so far:

Adding formtarget="_blank" to the button tag

Adding target="_blank" to the button tag

Adding , new { target = "_blank" } to the url.action

Neither of those seemed to do the trick. What am I missing/ doing wrong here?

I managed to fix it by changing the button tag to an a tag:

<a href="@Url.Action("/DetailToPdf/"+ comb.Id, "Combined")"  class="btn btn-warning" target="_blank"><span class="glyphicon glyphicon-download-alt" aria-hidden="true"></span></a>

I will leave the question open a bit longer to see if people know a better way to go about this or not.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM