0

I've have a byte array that has all the data needed and can be displayed in a pdf that can be opened if I use this code:

System.IO.File.WriteAllBytes(@"C:\Users\Person\Downloads\results.pdf", bytes) 

That let's me know the byte array should be good. The issue is I'm trying to create a pdf on the fly and when I view the call in the network tab in dev tools, it shows a 200 and in the response tab I get a text response. When I copy that response in a text editor and save it as a pdf, it has the correct amount of pages, but no data in it. So I have two issues here. For some reason, it is not being returned as a pdf AND the byte array is not being written to it. Any suggestions on what I can do? I've looked at stack overflow all day and have tried all kinds of things including setting the contentType to application/pdf.

byte[] bytes = ms.ToArray();
HttpResponse response = HttpContext.Current.Response;
response.Clear();
response.Buffer = true;
response.AddHeader("Accept-Header", bytes.Length.ToString());
response.AddHeader("Content-Length", bytes.Length.ToString());
response.AddHeader("Content-Disposition", "attachment;filename=Results.pdf");
response.AddHeader("Content-Description", "File Download");
response.ContentType = "application/octet-stream";
response.OutputStream.Write(bytes, 0, onvert.ToInt32(bytes.Length));

//I've tried using this as well
//response.BinaryWrite(bytes);
response.Flush();
response.End();

UPDATED CODE:

     byte[] bytes = ms.ToArray();

        HttpResponse response = HttpContext.Current.Response;
        try
        {
            response.Clear();
            response.ClearHeaders();
            response.ClearContent();
            response.AddHeader("Accept-Header", bytes.Length.ToString());
            response.AddHeader("Content-Length", bytes.Length.ToString());
            response.AddHeader("Content-Disposition", "attachment;filename=Results.pdf");
            response.ContentType = "application/pdf";
            response.BinaryWrite(bytes);
            response.Flush();
        }
        catch { }
        finally
        {
            response.End();
        }
  • 1
    [The proper MIME type for PDF's is `application/pdf`](https://stackoverflow.com/questions/312230/proper-mime-media-type-for-pdf-files) (I know you tried this, just confirming that this is what you should set it to, not `application/octet-stream`.) – TypeIA Jan 22 '18 at 22:37
  • 1
    “Copy in a text editor and save it”, that sounds like a probable reason for this. – Sami Kuhmonen Jan 22 '18 at 22:39
  • Update: Ok, so I've changed the mime type back to "application/pdf" and if I run it in IE11 under the network tab for response body it says the data could not be rendered but it gives me a save as link so when I save as a pdf and then view it, it is complete. So it appears that there is some strange broswer issue going on? – graphical_force Jan 22 '18 at 22:48
  • why do you set `Accept-Header` to the content length? That does not make sense. – mkl Jan 23 '18 at 05:21
  • Update: I've updated the code to work in IE11 but I have to manually save the pdf in the network tools. I get nothing that asks me if I want to download it like it should with the Content-Disposition set with attachment. What might I be missing? – graphical_force Jan 23 '18 at 15:15

1 Answers1

0

I got it working with this:

            response.Clear();
            response.ClearHeaders();
            response.ClearContent();
            response.ContentType = "application/pdf";
            response.AddHeader("content-disposition", "attachment; filename=Results.pdf");
            response.BinaryWrite(bytes);
            response.Flush();
            response.Close();