Javascript 如何在javascript中打开pdf文件新浏览器窗口

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7727045/
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-24 03:21:21  来源:igfitidea点击:

How to open a pdf file new browser window in javascript

javascript

提问by Shades88

I need to open a pdf file in new browser tab. How to do this. I was using

我需要在新的浏览器选项卡中打开一个 pdf 文件。这该怎么做。我正在使用

var docLocation = '../downloads/doc.pdf';
window.open(docLocation,"resizeable,scrollbar"); 

But it opens a download dialog box of the browser. How to achieve this ?

但它会打开浏览器的下载对话框。如何实现这一目标?

采纳答案by Jamie Dixon

The ability to display the pdf is entirely dependent on whether the user has a plugin available to display the pdf and also has their settings set to treat pdf files this way.

显示 pdf 的能力完全取决于用户是否有可用于显示 pdf 的插件,以及他们的设置是否设置为以这种方式处理 pdf 文件。

There are some flash widgets out there that can be used to present pdf content to the user but to directly answer your question, you cannot control the users preferences for how they chose to handle pdf files.

有一些 flash 小部件可用于向用户呈现 pdf 内容,但要直接回答您的问题,您无法控制用户选择如何处理 pdf 文件的首选项。

回答by Bachask8

here

这里

    <a href="javascript:void(0);" onclick="javascipt:window.open('YourPDF.pdf');" class="popup">Clic to open.</a>

you need to have installed reader in your pc

你需要在你的电脑上安装阅读器

回答by BlackMagic

This code will open a pdf document in a full window from JavaScript

此代码将在一个完整的窗口中打开一个 pdf 文档 JavaScript

var pdf = MyPdf.pdf;
window.open(pdf);

A function to open windows would look like this:

打开窗口的函数如下所示:

function openPDF(pdf){
  window.open(pdf);
  return false;
}

回答by Andrey M.

Make sure the Content-Type header is 'application/pdf' and not 'application/octet-stream'

确保 Content-Type 标头是“application/pdf”而不是“application/octet-stream”

回答by aggaton

I tried all of the above solutions and none of them worked for me, I'm running javascript on top of mvc 3, and razor, adobe 11 installed as add-on on ie, Chrome and Firefox. Here is what I did in order to get it to work on all above browsers.

我尝试了上述所有解决方案,但没有一个对我有用,我在 mvc 3 之上运行 javascript,而 razor、adobe 11 作为附加组件安装在 ie、Chrome 和 Firefox 上。这是我为了让它在所有上述浏览器上工作而做的事情。

Made PDF controller, called from javascript like this

制作 PDF 控制器,像这样从 javascript 调用

in razor code for main view:

在主视图的剃刀代码中:

    var URL_OPEN_REPORT_PDF                   = "@Url.Content("~/Report/OpenPDF/")";

javascript:

javascript:

    var sURL = URL_OPEN_REPORT_PDF;
    sURL = AddURLParameter(sURL, "ReportArchive", moControl.treeOrganization.getUserData(sItemUI, "reportarchive"));
    window.open(sURL);

controller ReportController.cs:

控制器 ReportController.cs:

    [Authorize]
    [HttpGet]
    public ActionResult OpenPDF(string ReportArchive)
    {
        PDFResult oPdfResult = new PDFResult();

        ReportArchive oReportArchive;

        var serializer = new JavaScriptSerializer();
        oReportArchive = serializer.Deserialize<ReportArchive>(ReportArchive);
        string FilePath = Server.MapPath(string.Format("~/Content/Reports/{0}", oReportArchive.FileName));

        WebClient User = new WebClient();

        Byte[] FileBuffer = User.DownloadData(FilePath);

        if (FileBuffer != null)
        {
            oPdfResult.Length = FileBuffer.LongLength;
            oPdfResult.FileBuffer = FileBuffer;
            Response.BinaryWrite(FileBuffer);

        } return View("PDF", oPdfResult);
    }

ViewModel PDFResult.cs:

视图模型 PDFResult.cs:

public class PDFResult
{
    /// <summary>
    /// Content Length
    /// </summary>
    public long Length { get; set; }

    /// <summary>
    /// Content bytes
    /// </summary>
    public Byte[] FileBuffer { get; set; }
}

View PDF.cshtml:

查看PDF.cshtml:

@model Report.PDFResult
@{
    Response.ContentType = "application/pdf";
    Response.AddHeader("content-length", Model.Length.ToString()); 
    Layout = null;
}