How to extract file from an FTP directory?

0

Good afternoon guys,

I need to extract the files that are in the FTP folders. Following the examples that are in these links:

p>

FtpWebResponse.GetResponseStream returning an HTML

Downloading files from FTP

I managed to extract the name of the FTP folders, but now I need to know how to extract the files that are saved in these folders.

Can anyone help me?

Here is the code I'm using, which can be found in one of the links quoted above.

Thank you.

    public static void BUSCADIRTESTE(string uri, string usuario, string senha)
    {
        FtpWebRequest ftpRequest = (FtpWebRequest)WebRequest.Create(uri);
        ftpRequest.Credentials = new NetworkCredential(usuario, senha);
        ftpRequest.Method = WebRequestMethods.Ftp.ListDirectory;           

        FtpWebResponse response = (FtpWebResponse)ftpRequest.GetResponse();

        using (StreamReader streamReader = new StreamReader(response.GetResponseStream()))
        {
            List<string> directories = new List<string>();

            string line = streamReader.ReadLine();

            while (!string.IsNullOrEmpty(line))
            {
                directories.Add(line);
                line = streamReader.ReadLine();
            }
        }
    }
    
asked by anonymous 15.07.2016 / 23:21

2 answers

0

I was able to resolve it as follows:

    public static void StartDownloads(out string pstrMsg, out bool pbooRetorno, string pstrUrl, string pstrLocal, string pstrUsuario, string pstrSenha)
    {
        pstrMsg = string.Empty;
        pbooRetorno = false;

        try
        {
            // Get the object used to communicate with the server.
            FtpWebRequest request = (FtpWebRequest)WebRequest.Create(pstrUrl);
            request.Method = WebRequestMethods.Ftp.DownloadFile;

            // Credenciais utilizadas para conectar no servidor FTP
            request.Credentials = new NetworkCredential(pstrUsuario, pstrSenha);

            using (FtpWebResponse response = (FtpWebResponse)request.GetResponse())
            {
                Stream responseStream = response.GetResponseStream();

                using (StreamReader reader = new StreamReader(responseStream))
                {
                    // Cria o arquivo no local especificado
                    using (StreamWriter writer = new StreamWriter(pstrLocal))
                    {
                        string strLinhaArquivo = reader.ReadLine();

                        while (!String.IsNullOrEmpty(strLinhaArquivo))
                        {
                            // Gravar as informações no arquivo
                            writer.Write(strLinhaArquivo);

                            // Verifica se o arquivo extraido contém linha
                            if (!String.IsNullOrEmpty((strLinhaArquivo = reader.ReadLine())))
                            {
                                // Inseri uma nova linha no arquivo
                                writer.WriteLine();
                            }
                        }
                    }
                }
            }
            pbooRetorno = true;
        }
        catch (Exception ex)
        {
            pstrMsg = string.Format("Erro:\nMétodo 'StartDownloads'\nDetalhes: {0}", ex.Message);

            pbooRetorno = false;
        }
    }

Note: The method is only returning one file at a time. If you have to download all files at once, you have to adjust the method with necessary changes.

    
18.07.2016 / 21:23
0

I believe that the FTP protocol is not suitable for extracting files. When a file is extracted , the processing done is the server responsibility, ie you must send a command for the server to extract this file. In this context, you can perform the extraction using timed tasks or a programming language that interacts with the server. You can use PHP to extract files:

link

It is also possible in ASP.NET with the use of WebServices, but of course, it depends on the installed server.

    
17.07.2016 / 15:44