FtpWebResponse.ContentLength 속성

정의

FTP 서버로부터 받은 데이터의 길이를 가져옵니다.

public:
 virtual property long ContentLength { long get(); };
public override long ContentLength { get; }
member this.ContentLength : int64
Public Overrides ReadOnly Property ContentLength As Long

속성 값

FTP 서버로부터 받은 데이터의 바이트 수를 포함하는 Int64 값입니다.

예제

다음 코드 예제에서는 지정된 FTP 서버에서 파일을 다운로드합니다. 이 속성은 다운로드한 파일의 바이트 수를 포함합니다.

static bool DownloadFileFromServer( Uri^ serverUri, String^ localFileName )
{
   // The serverUri parameter should start with the ftp:// scheme.
   if ( serverUri->Scheme != Uri::UriSchemeFtp )
   {
      return false;
   }

   // Get the object used to communicate with the server.
   // Note that the cast to FtpWebRequest is done only
   // for the purposes of illustration. If your application
   // does not set any properties other than those defined in the
   // System.Net.WebRequest class, you can use the following line instead:
   // WebRequest request = WebRequest.Create(serverUri);
   //
   FtpWebRequest^ request = dynamic_cast<FtpWebRequest^>(WebRequest::Create( serverUri ));
   request->Method = WebRequestMethods::Ftp::DownloadFile;
   FtpWebResponse^ response = dynamic_cast<FtpWebResponse^>(request->GetResponse());
   Stream^ responseStream = nullptr;
   StreamReader^ readStream = nullptr;
   StreamWriter^ writeStream = nullptr;
   try
   {
      responseStream = response->GetResponseStream();
      readStream = gcnew StreamReader( responseStream,System::Text::Encoding::UTF8 );
      
      // Display information about the data received from the server.
      Console::WriteLine( "Bytes received: {0}", response->ContentLength );
      Console::WriteLine( "Message from server: {0}", response->StatusDescription );
      Console::WriteLine( "Resource: {0}", response->ResponseUri );

      // Write the bytes received from the server to the local file.
      if ( readStream != nullptr )
      {
         writeStream = gcnew StreamWriter( localFileName,false );
         writeStream->Write( readStream->ReadToEnd() );
      }
   }
   finally
   {
      if ( readStream != nullptr )
      {
         readStream->Close();
      }

      if ( response != nullptr )
      {
         response->Close();
      }

      if ( writeStream != nullptr )
      {
         writeStream->Close();
      }
   }

   return true;
}
public static bool DownloadFileFromServer(Uri serverUri, string localFileName)
{
    // The serverUri parameter should start with the ftp:// scheme.
    if (serverUri.Scheme != Uri.UriSchemeFtp)
    {
        return false;
    }
    // Get the object used to communicate with the server.
    // Note that the cast to FtpWebRequest is done only
    // for the purposes of illustration. If your application
    // does not set any properties other than those defined in the
    // System.Net.WebRequest class, you can use the following line instead:
    // WebRequest request = WebRequest.Create(serverUri);
    //
    FtpWebRequest request = (FtpWebRequest)WebRequest.Create(serverUri);
    request.Method = WebRequestMethods.Ftp.DownloadFile;

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

    Stream responseStream = null;
    StreamReader readStream = null;
    StreamWriter writeStream = null;
    try
    {
        responseStream = response.GetResponseStream();
        readStream = new StreamReader(responseStream, System.Text.Encoding.UTF8);
        // Display information about the data received from the server.
        Console.WriteLine("Bytes received: {0}",response.ContentLength);

        Console.WriteLine("Message from server: {0}", response.StatusDescription);
        Console.WriteLine("Resource: {0}", response.ResponseUri);

        // Write the bytes received from the server to the local file.
        if (readStream != null)
        {
            writeStream = new StreamWriter(localFileName, false);
            writeStream.Write(readStream.ReadToEnd());
        }
    }
    finally
    {
        if (readStream != null)
        {
            readStream.Close();
        }
        if (response != null)
        {
            response.Close();
        }
        if (writeStream != null)
        {
            writeStream.Close();
        }
    }
    return true;
}

설명

응답 스트림이 FTP 서버에서 반환되면 속성에는 ContentLength 스트림의 바이트 수가 포함됩니다. ContentLength 는 응답에 데이터가 반환되지 않았거나 서버가 콘텐츠 길이 정보를 보내지 않은 경우 -1을 반환합니다. 데이터가 반환되었거나 반환되어야 하는 경우 반환 값은 0보다 크거나 같습니다. 예를 들어 필드를 사용하는 요청의 ListDirectory 경우 속성은 ContentLength 항상 -1을 반환합니다. 메서드를 사용하는 요청의 UploadFile 경우 속성은 ContentLength 항상 0입니다. 메서드를 사용하는 요청의 DownloadFile 경우 다운로드한 파일에 데이터가 포함된 경우 속성이 0보다 크고 비어 있는 경우 0보다 큽니다.

메서드 ContentLength 를 사용하는 요청의 GetFileSize 경우 는 서버에서 지정된 파일의 크기를 반환합니다.

적용 대상

추가 정보