I am using MigraDoc to generate PDF, it saves perfectly on the local machine, but when I send this file stream to webapi, browser opens it with blank content.
var response = new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new StreamContent(new MemoryStream(pdfByteArray))
};
response.Content.Headers.ContentDisposition =
new ContentDispositionHeaderValue("attachment")
{
FileName = "MyPdf.pdf"
};
response.Content.Headers.ContentType =
new MediaTypeHeaderValue(@"application/pdf")
response.Content.Headers.ContentLength = pdfByteArray.Length;
return response;
When I open developer tools in Chrome I see that some response characters look like this:
Although when I try to create a local file using the same byte stream:
using (Stream stream = File.Create(@"d:\work\Test.pdf"))
{
new MemoryStream(pdfByteArray).CopyTo(stream);
}
It displays correctly the content of the PDF.
I was trying to specify different CharSet in Response Content-Type (like UTF-7, UTF-8, windows-1252 etc) and see that response chars are changed, but the result is same - I see blank PDF. Another note that when I send Json with pdfByteArray encoded in Base64, on UI I am able to decode this and create correct PDF. I am trying to find a way to send application/pdf content and let the browser render PDF correctly.