如何在新标签页上打开.pdf文件

时间:2021-05-05 21:17:06

The Objective:

I have to print a PDF on a new tab after some tasks have finished correctly.

在某些任务正确完成后,我必须在新选项卡上打印PDF。

Steps: I want to execute a method that should go to the server, take the PDF and open it on a new Tab, I was trying with these but is not working:

步骤:我想执行一个应该进入服务器的方法,获取PDF并在新的Tab上打开它,我正在尝试使用这些但是不起作用:

Controller: Export

 public ActionResult PrintPdf()
    {
        Response.AppendHeader("Content-Disposition", "inline; filename= " + MyClassPdfWriter.GetFileName);
        return File(MyClassPdfWriter.GetMemoryStream, "application/pdf");
    }

View:

function TasksSucced(){
      $.get('@Url.Action("PrintPdf", "Export", "new {target = _blank}")');
}

4 个解决方案

#1


13  

Solved! That works for me

解决了!这对我行得通

 window.open('/Export/PrintPdf');

#2


2  

$("a[target!='_blank'][href$='.pdf']").attr("target", "_blank");

#3


2  

If anyone is having a similar problem, none of the above solutions worked for me in Google Chrome (they did work in Firefox)

如果有人遇到类似的问题,上述解决方案都不适用于Google Chrome(他们确实在Firefox中有效)

This code worked in all major browsers:

此代码适用于所有主流浏览器:

var a = document.createElement('A');
var filePath = 'https://pathtopdf.com/file.pdf';
a.href = filePath;
a.download = filePath.substr(filePath.lastIndexOf('/') + 1);
document.body.appendChild(a);
a.click();
document.body.removeChild(a);

#4


1  

You can use the following solution

您可以使用以下解决方案

   jQuery('<form target="_blank" action="' + URL + '" method="get"></form>').appendTo('body').submit().remove();

where URL is the pdf url...

其中URL是pdf网址...

#1


13  

Solved! That works for me

解决了!这对我行得通

 window.open('/Export/PrintPdf');

#2


2  

$("a[target!='_blank'][href$='.pdf']").attr("target", "_blank");

#3


2  

If anyone is having a similar problem, none of the above solutions worked for me in Google Chrome (they did work in Firefox)

如果有人遇到类似的问题,上述解决方案都不适用于Google Chrome(他们确实在Firefox中有效)

This code worked in all major browsers:

此代码适用于所有主流浏览器:

var a = document.createElement('A');
var filePath = 'https://pathtopdf.com/file.pdf';
a.href = filePath;
a.download = filePath.substr(filePath.lastIndexOf('/') + 1);
document.body.appendChild(a);
a.click();
document.body.removeChild(a);

#4


1  

You can use the following solution

您可以使用以下解决方案

   jQuery('<form target="_blank" action="' + URL + '" method="get"></form>').appendTo('body').submit().remove();

where URL is the pdf url...

其中URL是pdf网址...