file does not begin with %pdf-
<p>I asked <a href="http://stackoverflow.com/questions/7101361/return-pdf-to-webpage-from-ashx">this question</a> and have now got it mostly working.</p>

<p>However I am still getting the titled error and cannot seem to find any reference to anyone that has solved it.</p>

<p>This is driving me insane!</p>

<p>My ASHX C# code looks like this;</p>

<pre><code>context.Response.Clear();
context.Response.ClearContent();
context.Response.ClearHeaders();
context.Response.Buffer = true;
context.Response.Charset = "";
context.Response.AddHeader("Content-Disposition", "inline;filename=file.pdf"); 
context.Response.ContentType = "application/pdf";

Stream fileStream = publishBookManager.GetFile();
byte[] buffer = new byte[32 * 1024];
using (MemoryStream ms = new MemoryStream())
{
  int read;
  while ((read = fileStream.Read(buffer, 0, buffer.Length)) &gt; 0)
  {
    ms.Write(buffer, 0, read);
  }
}

context.Response.BinaryWrite(buffer);
context.Response.Flush();
context.Response.Close();
</code></pre>

<p>And my jQuery / javascript looks like;</p>

<pre><code>window.open('/UserControls/download.ashx?format=pdf&amp;bookId=' + bookId, "pdfViewer");
</code></pre>
