在新标签页中打开PDF

时间:2014-04-08 22:30:43

标签: c# angularjs google-chrome servicestack internet-explorer-11

我需要使用servicestack在新窗口中打开PDF。我有一个PDF的MemoryStream,能够将PDF下载到浏览器。我的问题是我无法计算如何在新标签中打开PDF。我已使用此code从服务堆栈下载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();
    }
}

这是我正在使用的锚标签:

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

1 个答案:

答案 0 :(得分:5)

您正在使用内容处置作为附件。将其更改为内嵌,如此

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

这将在浏览器窗口中打开pdf文件。但请注意,您的浏览器必须具有可以打开pdf文件的插件。