简体   繁体   中英

Open a PDF in a new tab

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>

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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