在C#中使用ftp下载文件

时间:2018-07-25 11:50:52

标签: c# ftp

作为初级开发人员,我应该找到一种使用ftp下载文件的解决方案,并且我有此代码。
它可以工作,但是有时我无法打开下载的文件。

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;
}

请提出任何想法!

2 个答案:

答案 0 :(得分:6)

您为什么不使用它? WebClient由Microsoft实现,可以从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");

}

答案 1 :(得分:0)

检查无法打开的文件是否已损坏。 例如,ftp和本地PC上的文件大小是否相同?

您应该检查读者的阅读是否结束!

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;
}