5

I've got a C# page that's generating a PDF file and returning it to the user. I'm explicitly setting the Content-Type header to "application/pdf" and the MIME Type is registered in IIS, yet IIS seems to be stripping off the Content-Type.

The file is being returned correctly and if I choose to save it to disk I can open it just fine. If I run the page from the ASP.NET Development Server the Content-Type header comes through just fine.

The code...

byte[] pdf = //magic!
string filename = "Some.pdf";

Response.Clear();
Response.ClearHeaders();
//This way didn't work either...
//Response.ContentType = "application/pdf";
Response.AddHeader("Content-Type", "application/pdf");
Response.AddHeader("Content-Disposition", "attachment; filename=" + filename + ";size=" + pdf.Length.ToString());
Response.Flush();
Response.BinaryWrite(pdf);
Response.Flush();
Response.End();
MyItchyChin
  • 13,733
  • 1
  • 24
  • 44

1 Answers1

1

I have something very similar currently up and running (it lets user download a TXT file generated in the code behind itself).

Same code as yours basically but I don't have Response.Flush() anywhere. You might try commenting both .Flushes out and see what happens.

edited this is my code (successfully lets the user download a TXT file

string filename = "myfile.txt"; //made up filename
Response.AddHeader("Content-disposition", "attachment; filename=" + filename);
Response.ContentType = "application/octet-stream";

byte[] data = new byte[Encoding.UTF8.GetByteCount(_r)]; //_r is a string containing my txt
data = Encoding.UTF8.GetBytes(_r);
Response.ContentEncoding = Encoding.UTF8; // handling special chars
Response.BinaryWrite(data); 
Response.End();
Alex
  • 23,004
  • 4
  • 39
  • 73