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

屬性值

Int64 值,包含從 FTP 伺服器接收之資料的位元組數。

範例

下列程式碼範例會從指定的 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。 如果資料已傳回或應該傳回,則傳回值大於或等於零。 例如,對於使用 ListDirectory 欄位的要求, ContentLength 屬性一律會傳回 -1。 對於使用 UploadFile 方法的要求, ContentLength 屬性一律為零。 對於使用 DownloadFile 方法的要求,如果下載的檔案包含資料,則 屬性大於零,如果檔案是空的,則為零。

對於使用 GetFileSize 方法的要求, ContentLength 傳回伺服器上的指定檔案大小。

適用於

另請參閱