Link to home
Get AccessLog in
Avatar of shaileshmark
shaileshmark

asked on

ASP.NET pdf file download

Unable to open adobe pdf files downloaded from an asp.net 2.0 page. "the file is damaged and cannot be repaired" says adobe acrobat (7.0.9). The same file opens alright when opened in the browser with a Response.Redirect(FileURL). But when downloaded either through Response.WriteFile(Path) or Response.TransmitFile(Path) -- the entire file gets downloaded alright but adobe cannot open it.
Avatar of Brett Crawley
Brett Crawley

You need to set the content type of the http response before streaming the data to application/pdf

Regards
Try this:

string DownloadedFileName = "PDFName.pdf";
string PathOfFile = "/PDFName.pdf";

//Get the file from the server and store it
byte[] temp = System.IO.File.ReadAllBytes(Server.MapPath(PathOfFile));

Response.Clear();
Response.ContentType = "application/PDF";
Response.AddHeader("Content-Type", "application/PDF");
Response.AddHeader("Content-Disposition", "attachment;filename=" + DownloadedFileName);
Response.BinaryWrite(PDFimage);
Response.Flush();
Response.End();
Avatar of shaileshmark

ASKER

Okay, I should have posted my code

Response.ContentType = "application/pdf";
                Response.AppendHeader ( "Content-Disposition", "attachment; filename=\"" + FileName + "\"" );
                //Response.WriteFile ( FilePath);
                Response.TransmitFile ( FilePath);

strickdd

What is PDFimage in Response.BinaryWrite(PDFimage) in your code above? Is that temp byte[]?
ASKER CERTIFIED SOLUTION
Avatar of strickdd
strickdd
Flag of United States of America image

Link to home
membership
This content is only available to members.
To access this content, you must be a member of Experts Exchange.
Get Access