• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1991
  • Last Modified:

PDF file is damaged | Asp.net FileStream


When PDF files are downloaded, the user receives the following error:

"There was an error opening this document. The file is damaged and could not be repaired".

This error occurs with older versions of Adobe Acrobat Reader (tested and failed on 6.0, 7.0). The code I am using is pasted below and is a collaboration of research I've done on EE and google.

Note: The file that is streamed to the user has a higher file size than the actual file. You can see below I have tried using different headers, pdf and octet-stream.

We must maintain the ability of allowing the client to download the pdf file rather than it opening in the browser.

Any help is appreciated.

FileStream objfilestream = new FileStream(@"C:\MFiles\" + Session["FilePath"].ToString(), FileMode.Open, FileAccess.Read);
                int FileSize = (int)objfilestream.Length;
                byte[] getContent = new byte[FileSize];
                objfilestream.Read(getContent, 0, FileSize);
                Response.AddHeader("Content-Length", FileSize.ToString());
                Response.AddHeader("content-disposition", "attachment; filename=" + Session["FilePath"].ToString());
                //Response.ContentType = "application/pdf";
                Response.ContentType = "application/octet-stream";

Open in new window

1 Solution
Be sure to end the stream after binary write with Response.End(); statement.
drbill02Author Commented:
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now