Khan
Khan

Reputation: 18162

How to tell when a file transfer has completed (Server to Client)

I am pulling a file from an ftp server and I'm having trouble feeling comfortable with my method of verifying the transfer was completed successfully.

It feels like there must be a more concrete way of detecting a successful transfer. Any ideas?

My Code:

            var request = (FtpWebRequest)FtpWebRequest.Create(ftpFilePath);

            request.KeepAlive = false;
            request.UseBinary = true;
            request.UsePassive = false;
            request.Credentials = new NetworkCredential("Username", "Password");
            request.Method = WebRequestMethods.Ftp.DownloadFile;

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

            using (var stream = response.GetResponseStream())
            {
                using (var reader = new StreamReader(stream))
                {
                    contents = reader.ReadToEnd();
                }
            }

            //Check to see if transfer was successful
            if (response.StatusDescription.StartsWith("2"))
                transferSuccessful = true;

Upvotes: 4

Views: 3757

Answers (1)

Peter Ritchie
Peter Ritchie

Reputation: 35870

Check the FtpWebResponse.StatusCode for success. e.g. FtpStatusCode.ClosingData

Upvotes: 5

Related Questions