Link to home
Start Free TrialLog in
Avatar of drbill02
drbill02Flag for United States of America

asked on

PDF file is damaged | Asp.net FileStream

Hello,

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.

Thanks.
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);
                objfilestream.Close();
 
                Response.AddHeader("Content-Length", FileSize.ToString());
                Response.AddHeader("content-disposition", "attachment; filename=" + Session["FilePath"].ToString());
                //Response.ContentType = "application/pdf";
                Response.ContentType = "application/octet-stream";
                Response.BinaryWrite(getContent);

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of ragulin
ragulin
Flag of Czechia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of drbill02

ASKER

Thanks!!!