Question

I need to open a PDF in a new window using servicestack. I have a MemoryStream of the PDF and able to download the PDF to the browser. My problem is I can't figure how to open the PDF in a new tab. I have used this code to download the pdf from service stack.

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>
Was it helpful?

Solution

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. but note that your browser must have plugin that can open pdf file.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top