简体   繁体   English

在新标签页中打开PDF

[英]Open a PDF in a new tab

I need to open a PDF in a new window using servicestack. 我需要使用servicestack在新窗口中打开PDF。 I have a MemoryStream of the PDF and able to download the PDF to the browser. 我有PDF的MemoryStream,并且能够将PDF下载到浏览器。 My problem is I can't figure how to open the PDF in a new tab. 我的问题是我不知道如何在新标签页中打开PDF。 I have used this code to download the pdf from service stack. 我已使用此代码从服务堆栈下载pdf。

public class PDfResult : IDisposable, IStreamWriter, IHasOptions
{
    private readonly Stream _responsestream = null;
    public IDictionary<string, string> Options { get; set; }

    public PDfResult(Stream responseStream)
    {
        _responsestream = responseStream;
        Options = new Dictionary<string, string>
        {
            {"Content-Type", "application/pdf"},
            {"Content-Disposition", "attachment; filename=\"mypdf.pdf\";"}
        };
    }

    public void WriteTo(Stream responseStream)
    {
        if (_responsestream == null)
            return;

        _responsestream.WriteTo(responseStream);
        responseStream.Flush();
    }

    public void Dispose()
    {
        _responsestream.Dispose();
    }
}

This is the anchor tag I am using: 这是我正在使用的定位标记:

<a class="btn" href="api/myapi?rosType=blank&amp;rId=0&amp;rType=Daily&amp;Pages=1&amp;Weeks=1" target="_blank">Generate PDF</a>

You are using content-disposition as attachment. 您正在使用内容处置作为附件。 change it to inline like this 像这样将其更改为内联

    Options = new Dictionary<string, string>
    {
        {"Content-Type", "application/pdf"},
        {"Content-Disposition", "inline; filename=\"mypdf.pdf\";"}
    };

This will open pdf file within browser window. 这将在浏览器窗口中打开pdf文件。 but note that your browser must have plugin that can open pdf file. 但请注意,您的浏览器必须具有可以打开pdf文件的插件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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