T
Tim Cowan
Hi,
I am trying to download a 3MB file from an MS FTP server. Below is the code.
Everything works fine, it starts the download and gets to with a couple of
thousand bytes and then just hangs and eventually times out. It happens with
4MB files too. Small files under 100k work just fine. I haven't tested much
in between .
If I connect using a simple FTP client it works just fine and downloads the
file. I have no idea why it will not finish the download. I am using VS 2005
on Win XP.
Can someone help?
Tim
public bool DownloadFile(string fileName, string mfgrId)
{
try
{
Uri uri = new Uri("ftp://" + hostname + downloadDir + fileName);
FtpWebRequest request = WebRequest.Create(uri) as FtpWebRequest;
request.Credentials = GetCredentials();
ftp = request;
ftp.Method = WebRequestMethods.Ftp.DownloadFile;
ftp.KeepAlive = false;
ftp.UseBinary = true; //true binary false text
ftp.UsePassive = true;
using (WebResponse response = ftp.GetResponse())
using (BinaryReader reader = new BinaryReader(response.GetResponseStream()))
using (BinaryWriter writer = new BinaryWriter(File.Open(@"C:\Program
Files\MarginMate\" + fileName, FileMode.Create)))
{
byte[] buffer = new byte[2048];
int count = reader.Read(buffer, 0, buffer.Length);
while (count != 0)
{
writer.Write(buffer, 0, count);
count = reader.Read(buffer, 0, buffer.Length);
}
}
ErrorReporter.LogableError(fileName + " downloaded successfully.",
ErrorReporter.ErrorTypes.DDS);
return true;
}
catch (Exception e)
{
ErrorReporter.LogableError("Failed to download file: " +
e.Message.ToString(), ErrorReporter.ErrorTypes.DDS);
return false;
}
}
I am trying to download a 3MB file from an MS FTP server. Below is the code.
Everything works fine, it starts the download and gets to with a couple of
thousand bytes and then just hangs and eventually times out. It happens with
4MB files too. Small files under 100k work just fine. I haven't tested much
in between .
If I connect using a simple FTP client it works just fine and downloads the
file. I have no idea why it will not finish the download. I am using VS 2005
on Win XP.
Can someone help?
Tim
public bool DownloadFile(string fileName, string mfgrId)
{
try
{
Uri uri = new Uri("ftp://" + hostname + downloadDir + fileName);
FtpWebRequest request = WebRequest.Create(uri) as FtpWebRequest;
request.Credentials = GetCredentials();
ftp = request;
ftp.Method = WebRequestMethods.Ftp.DownloadFile;
ftp.KeepAlive = false;
ftp.UseBinary = true; //true binary false text
ftp.UsePassive = true;
using (WebResponse response = ftp.GetResponse())
using (BinaryReader reader = new BinaryReader(response.GetResponseStream()))
using (BinaryWriter writer = new BinaryWriter(File.Open(@"C:\Program
Files\MarginMate\" + fileName, FileMode.Create)))
{
byte[] buffer = new byte[2048];
int count = reader.Read(buffer, 0, buffer.Length);
while (count != 0)
{
writer.Write(buffer, 0, count);
count = reader.Read(buffer, 0, buffer.Length);
}
}
ErrorReporter.LogableError(fileName + " downloaded successfully.",
ErrorReporter.ErrorTypes.DDS);
return true;
}
catch (Exception e)
{
ErrorReporter.LogableError("Failed to download file: " +
e.Message.ToString(), ErrorReporter.ErrorTypes.DDS);
return false;
}
}