简体   繁体   中英

How can I save files from ftp?

I have a methode that copy folder structure from ftp to local folder and then copy all files that consists in them:

public void CreateDirectories()
{
    FtpWebRequest request = (FtpWebRequest)WebRequest.Create(Url);
    request.Credentials = new NetworkCredential(Login, Pass);
    request.Method = WebRequestMethods.Ftp.ListDirectory;

    string soursePath = @"L:\Test";

    StreamReader streamReader = new StreamReader(request.GetResponse().GetResponseStream());

    string directoryName = streamReader.ReadLine();

    while (directoryName != null)
    {
        //Create directories structure
        if (directoryName.StartsWith("I") && !directoryName.Contains("p"))
        {
            string newPath = System.IO.Path.Combine(soursePath, directoryName);
            if (!System.IO.Directory.Exists(newPath))
            {
                System.IO.Directory.CreateDirectory(newPath);

                //get file list and invoke DownLoad(string directoryName, string fileName)
                FtpWebRequest fileRequest = (FtpWebRequest)WebRequest.Create(Url + directoryName + "/");
                fileRequest.Credentials = new NetworkCredential(Login, Pass);
                fileRequest.Method = WebRequestMethods.Ftp.ListDirectory;

                StreamReader fileStreamReader = new StreamReader(fileRequest.GetResponse().GetResponseStream());
                string fileName = fileStreamReader.ReadLine();
                while (fileName != null)
                {
                    DownLoad(directoryName, fileName);
                    fileName = streamReader.ReadLine();
                }

            }
        }

        directoryName = streamReader.ReadLine();
    }

    request = null;
    streamReader = null;
}

and the methode that copy current file:

public void DownLoad(string directoryName, string fileName)
{
    string localPath = @"L:\Test\" + directoryName;

    FtpWebRequest requestFileDownload = (FtpWebRequest)WebRequest.Create("ftp://ftp.equip.me/prod/" + directoryName + "/" + fileName);
    requestFileDownload.Credentials = new NetworkCredential(Login, Pass);
    requestFileDownload.Method = WebRequestMethods.Ftp.DownloadFile;

    FtpWebResponse responseFileDownload = (FtpWebResponse)requestFileDownload.GetResponse();

    Stream responseStream = responseFileDownload.GetResponseStream();
    FileStream writeStream = new FileStream(localPath + "\\" + fileName, FileMode.Create);

    int Length = 2048;
    Byte[] buffer = new Byte[Length];
    int bytesRead = responseStream.Read(buffer, 0, Length);

    while (bytesRead > 0)
    {
        writeStream.Write(buffer, 0, bytesRead);
        bytesRead = responseStream.Read(buffer, 0, Length);
    }

    responseStream.Close();
    writeStream.Close();

    requestFileDownload = null;
    responseFileDownload = null;
}

But in line Stream responseStream = responseFileDownload.GetResponseStream(); it stop for nearly 40 seconds and then throw an exeption of timeout, and no one file has not been saved (file is small - 50 kb)

The first thing that you should try is to turn passive mode off since this is automatically blocked by most firewalls, but is the default mode of operation for ftpWebRequest.

Just below this line:

requestFileDownload.Method = WebRequestMethods.Ftp.DownloadFile;

and this one:

requestFileDownload.UsePassive = false;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM