I have a URL to a PDF and I want to serve up the PDF to my page viewer.
I can successfully (I think) retrieve the PDF file. Then when I do the Response.BinaryWrite() I get a "The file is damaged and could not be repaired" error from the adobe reader.
Here's the code I have:
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
byte[] output = DoWork("Http://localhost/test.pdf");
Response.Clear();
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "inline; filename=MyPDF.PDF");
Response.AddHeader("content-length", output.Length.ToString());
Response.BinaryWrite(output);
Response.End();
}
}
public byte[] DoWork(string requestUrl)
{
byte[] responseData;
HttpWebRequest req = null;
HttpWebResponse resp = null;
StreamReader strmReader = null;
try
{
req = (HttpWebRequest)WebRequest.Create(requestUrl);
using (resp = (HttpWebResponse)req.GetResponse())
{
byte[] buffer = new byte[resp.ContentLength];
BinaryReader reader = new BinaryReader(resp.GetResponseStream());
reader.Read(buffer, 0, buffer.Length);
responseData = buffer;
}
}
finally
{
if (req != null)
{
req = null;
}
if (resp != null)
{
resp.Close();
resp = null;
}
}
return responseData;
}