Hello dharmendr,
I have change my code to download pdf and it work perfectly And thank you for your response.
this the new code
// stream the file byte on ftp
byte[] _downFile = DownloadFileFromFtp(pathFile);
if (_downFile != null && _downFile.Length > 0)
{
string _fname = visit_no + ".pdf";
Response.ContentType = "application/" + _fname.Split('.')[1];
Response.AddHeader("Content-disposition", "attachment; filename=" + _fname);
Response.OutputStream.Write(_downFile, 0, _downFile.Length);
string str = "<div class='alert alert-success text-center alert-dismissible fade show' role = 'alert' style='align - content:center; justify - content:center' > The Result <strong>downloaded successfully</ strong> <button type ='button' class='close' data-dismiss='alert' aria-label='Close'><span aria-hidden='true'>×</span></button></div>";
ltstatus.Text = str;
ltstatus.Visible = true;
Response.End();
}
public FtpWebRequest ConnectToFtp(string Method, string ftp)
{
string path = ftp;
FtpWebRequest ftpRequest = (FtpWebRequest)FtpWebRequest.Create(new Uri(path));
ftpRequest.UseBinary = true;
ftpRequest.Credentials = new NetworkCredential(username, password);
ftpRequest.Method = Method;
ftpRequest.Proxy = null;
ftpRequest.KeepAlive = false;
ftpRequest.UsePassive = false;
return ftpRequest;
}
public byte[] DownloadFileFromFtp(string ftpPath)
{
StringBuilder filesstring = new StringBuilder();
WebResponse webResponse = null;
try
{
FtpWebRequest ftpRequest = ConnectToFtp(WebRequestMethods.Ftp.DownloadFile, ftpPath);
ftpRequest.UseBinary = true;
webResponse = ftpRequest.GetResponse();
FtpWebResponse response = (FtpWebResponse)webResponse;
Stream dfileResponseStream = response.GetResponseStream();
int Length = 1024;
Byte[] buffer = new Byte[Length];
// collect all bytes of file
List<byte> filebytes = new List<byte>();
int bytesRead = dfileResponseStream.Read(buffer, 0, Length);
while (bytesRead > 0)
{
for (int i = 0; i < bytesRead; i++)
// read file byte in buffer and add it to list
filebytes.Add(buffer[i]);
bytesRead = dfileResponseStream.Read(buffer, 0, Length);
}
response.Close();
return filebytes.ToArray(); // return complete byte array
}
catch
{
//do any thing with exception
return null;
}
finally
{
if (webResponse != null)
{
webResponse.Close();
}
}
}