Click here to Skip to main content
15,889,595 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi there,
I have an issue in that some PDF files are not opening up while others open quite nicely. It doesn't seem to be a file size issue either as the files I'm trying to open are quite small, less than or about one MB. The error given while opening is that the files are corrupt and could not be repaired.
The Code which I am using to open the PDF along with any other type of file is as shown below.

Please help me!
C#
FileInfo infofile = new FileInfo(Filename);
string myFile = Filename;
WebClient myWeb = new WebClient();
Byte[] myBuff = myWeb.DownloadData(myFile);
Response.Expires = 0;
Response.CacheControl = "no-cache";
Response.AppendHeader("Pragma", "no-cache");
Response.Buffer = true;
Response.ContentType = contenttype;//"application/pdf";
Response.AddHeader("Content-Disposition", "attachment; filename=" + infofile.Name);
Response.BinaryWrite(myBuff);

Thanks
Posted
Updated 30-Jan-12 21:59pm
v4

suhailnoorpawane wrote:
The Code which I am using to open the PDF along with any other type of file is as shown below

If you are opening files other than PDFs with your code you are possibly having a problem determining the correct mime type to set into the ContentType header. Please do the following: Using Fiddler2[^] work out which mime-type is being sent when opening up the failing documents from the browser. This could very well help you determine the cause of your troubles. Another thing for you to attempt is to use thorough logging in your application writing the interesting titbits into a file for analaysis when things go astray.

Regards,

Manfred
 
Share this answer
 
"The error given while opening is that the files are corrupt and could not be repaired."

There you go. The answer lies there.


If some PDF's are opening then the code obviously works.
The problem therefore lies in the PDF's and not in your code.

Try obtaining the PDF's from the source again.
 
Share this answer
 
Hi,
Can you check the pdf versions of the files that is not open, and try to match with the pdf version that has installed in your computer. It might cause version problem.
 
Share this answer
 
I think it was getting remained in the Buffer.
So i just added it to the above Code.

C#
Response.Flush();
Response.Close();

And this did the trick for me

Any Ways Thanks To all who gave a thought to that.
 
Share this answer
 
v2
Comments
Anuja Pawar Indore 31-Jan-12 6:20am    
Added pre tag

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900