如何判断文件传输何时完成(服务器到客户端)

时间:2012-05-18 13:29:46

标签: .net ftpwebrequest

我正在从ftp服务器中提取文件,而我在验证传输成功完成的方法时遇到了麻烦。

感觉必须有更具体的方法来检测成功转移。有什么想法吗?

我的代码:

            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;

1 个答案:

答案 0 :(得分:5)

检查FtpWebResponse.StatusCode是否成功。例如FtpStatusCode.ClosingData