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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15944245/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 16:04:42  来源:igfitidea点击:

How to Open .pdf on a new Tab

javascriptjqueryasp.net-mvc-3url.action

提问by user1520494

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 并在新选项卡上打开它,我正在尝试使用这些但不起作用:

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}")');
}

回答by user1520494

Solved! That works for me

解决了!这对我行得通

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

回答by Timtech

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

回答by Jordash

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);

回答by Ephy L

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 url...

回答by Sergio Reis

There several ways to download or view the PDF.

有多种方法可以下载或查看 PDF。

  • View
  • 看法

You can set the content-typeas application/pdfin the response header just like Content-Type: application/pdf

您可以在响应标头中将内容类型设置为application/pdf就像Content-Type: application/pdf

And in order to open it to new tab in javascript, please add this code.

为了在javascript中将其打开到新选项卡,请添加此代码。

window.open("Here Download PDF url", '_blank');
  • Download
  • 下载

You need to set the content-typeas application/octet-streamin the response header just like application/octet-stream.

您需要在响应标头中将内容类型设置为application/octet-stream,就像application/octet-stream.

And add downloadHTML5 attribute to atag or just target="_blank".

并添加下载HTML5属性,一个标签或只是目标=“_空白”

<a href="PDF URL" download="pdf">Download</a>
<a href="PDF URL" target="_blank">Download</a>

So you can use PDF.js or 3rd library to view the PDF in iFrame or inline page.

因此,您可以使用 PDF.js 或第三个库在 iFrame 或内联页面中查看 PDF。

回答by dganenco

You can try using jQuery.deffered. I prepared sample function for you. It will process getPdf call, and then will resolve promise. Hope this helps

您可以尝试使用jQuery.defered。我为您准备了示例函数。它将处理 getPdf 调用,然后将解决承诺。希望这可以帮助

function getPDF() {
    return '';
}

getPdf()
  .then(function(response) {
    console.log('response here' + response);
    window.open(response);
  }).fail(function(){
      console.error('failed reposne');
  });