有没有办法用C#确认ftp上传?

时间:2013-08-14 05:50:06

标签: c# ftp

我想知道ftp进程是否可以给我任何上传成功确认。 我正在查看其中一个代码项目,除了潜在的异常 没有任何方法可以获得真正的成功确认。

public void upload(string remoteFile, string localFile)
    {
        try
        {
            ftpRequest = (FtpWebRequest)FtpWebRequest.Create(host + "/" + remoteFile);
            ftpRequest.Credentials = new NetworkCredential(user, pass);
            ftpRequest.UseBinary = true;
            ftpRequest.UsePassive = true;
            ftpRequest.KeepAlive = true;
            ftpRequest.Method = WebRequestMethods.Ftp.UploadFile;
            ftpStream = ftpRequest.GetRequestStream();
            FileStream localFileStream = new FileStream(localFile, FileMode.Create);
            byte[] byteBuffer = new byte[bufferSize];
            int bytesSent = localFileStream.Read(byteBuffer, 0, bufferSize);

            try
            {
                while (bytesSent != 0)
                {
                    ftpStream.Write(byteBuffer, 0, bytesSent);
                    bytesSent = localFileStream.Read(byteBuffer, 0, bufferSize);
                }
            }
            catch (Exception ex) { Console.WriteLine(ex.ToString()); }
            localFileStream.Close();
            ftpStream.Close();
            ftpRequest = null;
        }
        catch (Exception ex) { Console.WriteLine(ex.ToString()); }
        return;
    } 

1 个答案:

答案 0 :(得分:2)

文件上传后,但在您清理(关闭流,清空FtpWebRequest)之前,您可以像GetResponse()那样致电FtpWebRequest

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

FtpWebResponse对象包含StatusCode等字段,可以为您提供成功的指示。

完整代码示例:

以下是您重构的方法,具有上传状态(我实际上没有尝试过您的代码,因为它不能自行编译):

public void upload(string remoteFile, string localFile)
{
    FileStream localFileStream;
    FtpWebResponse ftpResponse;

    try
    {
        ftpRequest = (FtpWebRequest) FtpWebRequest.Create(host + "/" + remoteFile);
        ftpRequest.Credentials = new NetworkCredential(user, pass);
        ftpRequest.UseBinary = true;
        ftpRequest.UsePassive = true;
        ftpRequest.KeepAlive = true;
        ftpRequest.Method = WebRequestMethods.Ftp.UploadFile;
        ftpStream = ftpRequest.GetRequestStream();
        localFileStream = new FileStream(localFile, FileMode.Create);
        byte[] byteBuffer = new byte[bufferSize];
        int bytesSent = localFileStream.Read(byteBuffer, 0, bufferSize);

        while (bytesSent != 0)
        {
            ftpStream.Write(byteBuffer, 0, bytesSent);
            bytesSent = localFileStream.Read(byteBuffer, 0, bufferSize);
        }

        ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
        Console.WriteLine("Upload status: {0}, {1}", ftpResponse.StatusCode, ftpResponse.StatusDescription);
    }
    catch (Exception ex)
    {
        // log exception
        Console.WriteLine(ex.ToString());
        // throw;
    }
    finally
    {
        if (localFileStream != null)
        {
            localFileStream.Close();
        }
        if (ftpStream != null)
        {
            ftpStream.Close();
        }   
        if (ftpResponse != null)
        {
            ftpResponse.Close();
        }            
        ftpRequest = null;
    }
}