2

I'm trying to download files from my FTP server - multiples at the same time. When i use the DownloadFileAsync .. random files are returned with a byte[] Length of 0. I can 100% confirm the file exists on the server and has content AND there FTP server (running Filezilla Server) isn't erroring and say's the file has been transferred.

private async Task<IList<FtpDataResult>> DownloadFileAsync(FtpFileName ftpFileName)
{
    var address = new Uri(string.Format("ftp://{0}{1}", _server, ftpFileName.FullName));
    var webClient = new WebClient
    {
        Credentials = new NetworkCredential(_username, _password)
    };

    var bytes = await webClient.DownloadDataTaskAsync(address);
    using (var stream = new MemoryStream(bytes))
    {
        // extract the stream data (either files in a zip OR a file);
        return result;
    }
}

When I try this code, it's slower (of course) but all the files have content.

private async Task<IList<FtpDataResult>> DownloadFileAsync(FtpFileName ftpFileName)
{
    var address = new Uri(string.Format("ftp://{0}{1}", _server, ftpFileName.FullName));
    var webClient = new WebClient
    {
        Credentials = new NetworkCredential(_username, _password)
    };

    // NOTICE: I've removed the AWAIT and a different method.
    var bytes = webClient.DownloadData(address);
    using (var stream = new MemoryStream(bytes))
    {
        // extract the stream data (either files in a zip OR a file);
        return result;
    }
}

Can anyone see what I'm doing wrong, please? Why would the DownloadFileAsync be randomly returning zero bytes?

Pure.Krome
  • 84,693
  • 113
  • 396
  • 647
  • Have you tried `HttpClient` instead, just for the sake of it? – noseratio Apr 08 '14 at 12:56
  • Nope. there's a diff? – Pure.Krome Apr 08 '14 at 12:57
  • Nevermind about `HttpClient`, it's for HTTP only. OTOH, I did have a positive experience with `FtpClient`, wrapping it with `Task.Factory.FromAsync`: http://stackoverflow.com/a/21443538/1768303 – noseratio Apr 08 '14 at 13:26
  • Try using [WireShark](http://wireshark.org) or [Message Analyzer](http://www.microsoft.com/en-us/download/details.aspx?id=40308) to see what's going on the "wire". – Paulo Morgado Apr 09 '14 at 11:23

2 Answers2

1

Try out FtpWebRequest/FtpWebResponse classes. You have more available to you for debugging purposes.

FtpWebRequest - http://msdn.microsoft.com/en-us/library/system.net.ftpwebrequest(v=vs.110).aspx FtpWebResponse - http://msdn.microsoft.com/en-us/library/system.net.ftpwebresponse(v=vs.110).aspx

0

Take a look at http://netftp.codeplex.com/. It appears as though almost all methods implement IAsyncResult. There isn't much documentation on how to get started, but I would assume that it is similar to the synchronous FTP classes from the .NET framework. You can install the nuget package here: https://www.nuget.org/packages/System.Net.FtpClient/

Cameron Tinker
  • 9,634
  • 10
  • 46
  • 85