【问题标题】:download files from ftp to local drive将文件从 ftp 下载到本地驱动器
【发布时间】:2013-11-19 16:22:23
【问题描述】:

在 FTP 子文件夹中包含一些 csv 文件要下载到一个 csv 文件中的本地驱动器文件夹中。 在 FTP 中,每个 csv 文件只包含一条记录。所以,现在我想将所有 5 条记录放入一个 csv 文件中的 localdrive 文件夹中。这里的代码仅适用于一个 csv 文件。

   private void DownloadFile(string userName, string password, string ftpSourceFilePath, string localDestinationFilePath)
   {
       //FileStream responseStream =null;
        int Length = 2048;
        Byte[] buffer = new Byte[Length];


     //   int bytesRead = responseStream.Read(buffer, 0, Length);
        int bytesRead = 0;
        FtpWebRequest request = CreateFtpWebRequest(ftpSourceFilePath, userName, password, false);

        request.Method = WebRequestMethods.Ftp.DownloadFile;

        Stream reader = request.GetResponse().GetResponseStream();
        FileStream fileStream = new FileStream(localDestinationFilePath, FileMode.Create);

        while (true )
        {
            bytesRead = reader.Read(buffer,0,buffer.Length);
            if (bytesRead == 0)
                break;


            fileStream.Write(buffer, 0, bytesRead);
        }

        fileStream.Close();
    }
    private FtpWebRequest CreateFtpWebRequest(string ftpDirectoryPath, string userName, string password, bool keepAlive)
    {
        FtpWebRequest request = (FtpWebRequest)WebRequest.Create(new Uri(ftpDirectoryPath));

        //Set proxy to null. Under current configuration if this option is not set then the proxy that is used will get an html response from the web content gateway (firewall monitoring system)
        request.Proxy = null;

        request.UsePassive = true;
        request.UseBinary = true;
        request.KeepAlive = keepAlive;

        request.Credentials = new NetworkCredential(userName, password);

        return request;
    }


    private void button1_Click(object sender, EventArgs e)
    {
        DownloadFile("Username1", "Password1", "ftp://172.32.1.252:5010/Test/CBRE/building.csv", "C://Workspace/ex.csv");

    }

【问题讨论】:

    标签: ftp


    【解决方案1】:

    我不完全理解你想要做什么,但你也可以试试这个:

    WebClient Client = new WebClient ();
    Client.DownloadFile(http://www.domain.com/files/yourfile.ext, " yourfile.ext");
    

    WebClientClass

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-09-14
      • 1970-01-01
      • 1970-01-01
      • 2020-11-17
      • 1970-01-01
      • 1970-01-01
      • 2019-02-02
      相关资源
      最近更新 更多