0

So i have a PDF file on my server, i can open it up and it looks fine.

I then try to serve the file with this code

Response.ContentType = "Application/pdf";
Response.AppendHeader("Content-Disposition", "attachment; filename=Filename.pdf");
Response.TransmitFile("C:\\Temp\\Filename.pdf");
Response.End();

This serves the file, it saves on the client, but the problem is this new file is about .05Mb larger than the original, and when you try to open it the PDF is corrupt.

Any ideas?

user1024792
  • 553
  • 1
  • 10
  • 23
  • 2
    Possible same question ? http://stackoverflow.com/questions/9231239/response-transmitfile-corrupting-file – iPouf Aug 22 '13 at 22:24

1 Answers1

0

Your code should work. Please make sure you are using Ajax for downloading file.

You can try using flash and close before end.

Response.ContentType = "Application/pdf";
Response.AppendHeader("Content-Disposition", "attachment; filename=Filename.pdf");
Response.TransmitFile("C:\\Temp\\Filename.pdf");
Response.Flush();
Response.Close();
Response.End();

If it still doesn't work, you can try BinaryWrite. The only disadvantage is that it doesn't work well with large file because it loads the file to memory before sending to client.

Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment; filename=Filename.pdf"));
Response.BinaryWrite(File.ReadAllBytes("C:\\Temp\\Filename.pdf"));
Response.End();
Win
  • 61,100
  • 13
  • 102
  • 181