Click here to Skip to main content
15,886,026 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear All,
While download pdf file, following error appear and file unable to download..

Here my coding:
C#
string strFileName = @"D:\\File\1.pdf".ToString();
                   System.IO.FileStream fs = new System.IO.FileStream(strFileName, System.IO.FileMode.Open, System.IO.FileAccess.Read);
                   //fs = File.Open(strFileName, FileMode.Open);
                   byte[] bytBytes = new byte[(int)fs.Length];
                   fs.Read(bytBytes, 0, (int)fs.Length);
                   fs.Close();
                   Response.AddHeader("Content-disposition", "attachment; filename=" + strFileName);
                   Response.ContentType = "application/octet-stream";
                   Response.BinaryWrite(bytBytes);
                   Response.Flush();
                   Response.End();

I am using Visual studio 2008. Any namespace should add here?
Solve my issue ..
Thanks in Advance


Regards,
Palani Kumar.A
Posted

First, adding a namespace isn't going to do anything for you. This just shows that you have no idea what a namespace is or what it's used for.

Second, one of my greatest pet peeves is seeing people call .ToString() on a string. Come on!! Think about that!!

Third, your filename is an invalid path. As written, because of the @ character, your filename is "D:\\File\1.pfd". The double backslash near the beginning is causing a problem.

I think you're not understanding the difference between a string that should be treated literally and one that should be processed for escape sequences. Remove one of the backslashes from the doubled-up pair and it'll fix the problem.

Lastly, If you want to send a file in the response, you can shorten this code up greatly and drop reading the file. You don't need to read it at all:
string strFileName = @"D:\File\1.pdf";
Response.AppendHeader("content-disposition", "attachment; filename=" + strFileName);
Response.ContentType = "application/pdf";
Response.WriteFile(strFileName);



P.S.: What's with the Hungarian notation?? You know, the prepending of the variable type to the name of the variable?? That went the way of the dinosaurs a long time ago.
 
Share this answer
 
Dear All,

Thanks guys...

I have solved my issue myself.. Just remove the Update Panel from My Page, It is downloading the file...

Without Update Panel It will working fine....


Regards,
Palani Kumar.A
 
Share this answer
 

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