Reputation: 65308
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&rId=0&rType=Daily&Pages=1&Weeks=1" target="_blank">Generate PDF</a>
Upvotes: 0
Views: 2125
Reputation: 3798
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.
Upvotes: 5