3

As a junior developper, I should find a solution to download a file using ftp and I have this code.
It works but sometimes, I can not open the downloaded file.

public static bool DownloadDocument(string ftpPath, string downloadPath) {
  bool retVal = false;
  try {
    Uri serverUri = new Uri(ftpPath);
    if (serverUri.Scheme != Uri.UriSchemeFtp) {
        return false;
    }
    FtpWebRequest reqFTP;
    reqFTP = (FtpWebRequest)FtpWebRequest.Create(ftpPath);
    reqFTP.Credentials = new NetworkCredential(Tools.FtpUserName, Tools.FtpPassword);
    reqFTP.KeepAlive = false;
    reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
    reqFTP.UseBinary = true;
    reqFTP.Proxy = null;
    reqFTP.UsePassive = false;

    using (FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse()) {
      using (Stream responseStream = response.GetResponseStream()) {
        using (FileStream writeStream = new FileStream(downloadPath, FileMode.Create)) {
          int Length = 1024 * 1024 * 30;
          Byte[] buffer = new Byte[Length];
          responseStream.Read(buffer, 0, Length);
        }
      }
    }
    retVal = true;
  }
  catch (Exception ex) {
    //Error logging to add
  }

  return retVal;
}

Any ideas please!

Igor Cova
  • 3,126
  • 4
  • 31
  • 57
Tony_Clark
  • 33
  • 3
  • It is not clear what you say. I posted the actual code. I want to say that some I can't open some files. – Tony_Clark Jul 25 '18 at 11:56
  • 1
    Your code is missing *any* actual *write* to your output file. So saying "it works sometimes" is not remotely correct. – nvoigt Jul 25 '18 at 11:58
  • It's inane to have empty `catch (Exception ex)` blocks. It's bad enough catching the general `Exception` even when you do something with it. You should really avoid catching errors like this. – Enigmativity Jul 25 '18 at 12:01
  • `int Length = 1024 * 1024 * 30;` Maybe this is the problem. Having a static buffer does not seem good. It might be that the buffer is to small and thus the file is not written entirly. – M Stoerzel Jul 25 '18 at 12:03

2 Answers2

6

Why don't you use it?. WebClient implemented by Microsoft for a download from FTP.

using (WebClient client = new WebClient())
{
    client.Credentials = new NetworkCredential("log", "pass");
    client.DownloadFile("ftp://ftp.example.com/remote/path/file.zip", @"C:\local\path\file.zip");

}
Antoine V
  • 6,998
  • 2
  • 11
  • 34
0

Check if the file that you can not open is corrupted. For exemple, are the file size on ftp and your local pc the same ?

You should check if your reader reads to end !

public static bool DownloadDocument(string ftpPath, string downloadPath)
{
    bool retVal = false;
    try
    {
        Uri serverUri = new Uri(ftpPath);
        if (serverUri.Scheme != Uri.UriSchemeFtp)
        {
            return false;
        }
        FtpWebRequest reqFTP;
        reqFTP = (FtpWebRequest)FtpWebRequest.Create(ftpPath);
        reqFTP.Credentials = new NetworkCredential(Tools.FtpUserName, Tools.FtpPassword);
        reqFTP.KeepAlive = false;
        reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
        reqFTP.UseBinary = true;
        reqFTP.Proxy = null;
        reqFTP.UsePassive = false;

        using (FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse())
        {
            using (Stream responseStream = response.GetResponseStream())
            {
                using (FileStream writeStream = new FileStream(downloadPath, FileMode.Create))
                {
                    int Length = 1024 * 1024 * 30;
                    Byte[] buffer = new Byte[Length];
                  int byteReads =  responseStream.Read(buffer, 0, Length);
                  while(byteReads > 0)
                  {
                      //Try like this
                      writeStream.Write(buffer, 0, byteReads);
                      bytesRead = responseStream.Read(buffer, 0, Length);
                  }

                }
            }
        }
        retVal = true;
    }
    catch (Exception ex)
    {
       //Error logging to add
    }

    return retVal;
}
Coskun Ozogul
  • 2,389
  • 1
  • 20
  • 32