File DownLoad Save As Dialog From Server(Remote Machine),ASP.NET/C# ?

EDMS

New member
I am trying to download a File from Remote Machine using FTP and Save as Dialog. I am able to Download '.txt' File with its Contents. However, the same code doesnt work with other file types('.bmp','.jpg','.doc', etc.). I am able to use the save as dialog to download these files but either contents are **not** displayed or files are Corrupted.

The Following is my code:-


FtpWebRequest ftp;
try
{

FileStream outputStream = new FileStream(filePath + DwnLoadRename, FileMode.Create);
ftp = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftphost + "/" + DwnLoadRename)); //***------------------------>> ftphost = server ip***
ftp.Method = WebRequestMethods.Ftp.DownloadFile;
ftp.UseBinary = true;
ftp.Credentials = new NetworkCredential(uName, password);
FtpWebResponse response = (FtpWebResponse)ftp.GetResponse();
Stream ftpstream = response.GetResponseStream();
long clen = response.ContentLength;

int iFileSize = Convert.ToInt32(FileSize); //***----->> FileSize = Size of the file in bytes(derived from database)***
Byte[] bytBuffer = new byte[iFileSize];
int readCount = ftpstream.Read(bytBuffer, 0, iFileSize);
while (readCount > 0)
{
outputStream.Read(bytBuffer, 0, (readCount));
readCount = ftpstream.Read(bytBuffer, 0, iFileSize);

}


Response.Buffer = true;
Page.Response.Clear();
Page.Response.ClearHeaders();

Page.Response.ClearContent();

Page.Response.ContentType = Ctype; //***----------->> Ctype = ContentType(derived from the database)***
Page.Response.Charset = "UTF-8";


Page.Response.AddHeader("Content-Length", iFileSize.ToString());

Page.Response.AddHeader("Content-Disposition", "attachment; filename=" + DwnLoadRename);


Page.Response.BinaryWrite(bytBuffer);


Page.Response.Flush();



Page.Response.End();

ftpstream.Close();
outputStream.Close();
}

catch (Exception ex)
{
Response.Write(ex.Message);
}




Thanks
 
Back
Top