共用方式為


FtpWebRequest.ContentOffset 屬性

定義

取得或設定這個要求所下載之檔案的位元組位移。

public:
 property long ContentOffset { long get(); void set(long value); };
public long ContentOffset { get; set; }
member this.ContentOffset : int64 with get, set
Public Property ContentOffset As Long

屬性值

Int64 執行個體,指定檔案位移 (以位元組為單位)。 預設值為零。

例外狀況

由於已經在進行的要求,已為這個屬性指定新的值。

這個屬性的指定值小於零。

範例

下列程式代碼範例示範從伺服器下載部分檔案,並將下載的數據附加至本機檔案。

public:
   // NOT Working - throws a protocolError - 350 Restarting at 8. for args shown in Main.
   static bool RestartDownloadFromServer( String^ fileName, Uri^ serverUri, long offset )
   {
      // The serverUri parameter should use the ftp:// scheme.
      // It identifies the server file that is to be appended.
      // Example: ftp://contoso.com/someFile.txt.
      // 
      // The fileName parameter identifies the local file
      //
      // The offset parameter specifies where in the server file to start reading data.
      if ( serverUri->Scheme != Uri::UriSchemeFtp )
      {
         return false;
      }

      // Get the object used to communicate with the server.
      FtpWebRequest^ request = dynamic_cast<FtpWebRequest^>(WebRequest::Create( serverUri ));
      request->Method = WebRequestMethods::Ftp::DownloadFile;
      request->ContentOffset = offset;
      FtpWebResponse^ response = nullptr;
      try
      {
         response = dynamic_cast<FtpWebResponse^>(request->GetResponse());
      }
      catch ( WebException^ e ) 
      {
         Console::WriteLine( e->Status );
         Console::WriteLine( e->Message );
         return false;
      }

      Stream^ newFile = response->GetResponseStream();
      StreamReader^ reader = gcnew StreamReader( newFile );

      // Display downloaded data.
      String^ newFileData = reader->ReadToEnd();

      // Append the response data to the local file
      // using a StreamWriter.
      StreamWriter^ writer = File::AppendText(fileName);
      writer->Write(newFileData);

     // Display the status description.

     // Cleanup.
     writer->Close();
     reader->Close();
     response->Close();
     // string fileString = System.Text.Encoding.UTF8.GetString(newFileData);
     // Console::WriteLine( sr );
     Console::WriteLine("Download restart - status: {0}",response->StatusDescription);
     return true;
   }
public static bool RestartDownloadFromServer(string fileName, Uri serverUri, long offset)
{
    // The serverUri parameter should use the ftp:// scheme.
    // It identifies the server file that is to be downloaded
    // Example: ftp://contoso.com/someFile.txt.

    // The fileName parameter identifies the local file.
    //The serverUri parameter identifies the remote file.
    // The offset parameter specifies where in the server file to start reading data.

    if (serverUri.Scheme != Uri.UriSchemeFtp)
    {
        return false;
    }
    // Get the object used to communicate with the server.
    FtpWebRequest request = (FtpWebRequest)WebRequest.Create(serverUri);
    request.Method = WebRequestMethods.Ftp.DownloadFile;
    request.ContentOffset = offset;
    FtpWebResponse response = null;
    try
    {
        response = (FtpWebResponse) request.GetResponse();
    }
    catch (WebException e)
    {
        Console.WriteLine (e.Status);
        Console.WriteLine (e.Message);
        return false;
    }
    // Get the data stream from the response.
    Stream newFile = response.GetResponseStream();
    // Use a StreamReader to simplify reading the response data.
    StreamReader reader  = new StreamReader(newFile);
    string newFileData = reader.ReadToEnd();
    // Append the response data to the local file
    // using a StreamWriter.
    StreamWriter writer = File.AppendText(fileName);
    writer.Write(newFileData);
    // Display the status description.

    // Cleanup.
    writer.Close();
    reader.Close();
    response.Close();
    Console.WriteLine("Download restart - status: {0}",response.StatusDescription);
    return true;
}

備註

ContentOffset從 FTP 伺服器下載檔案時設定 屬性。 這個位移表示伺服器檔案中標示要下載之數據開頭的位置。 位移指定為檔案開頭的位元元數目;第一個字節的位移為零。

設定 ContentOffset 會導致將 FtpWebRequest 重新啟動 (REST) 命令傳送至伺服器。 如果您要將數據上傳至伺服器,大部分的 FTP 伺服器實作都會忽略此命令。

呼叫、 或方法之後變更ContentOffset會導致例外狀況。BeginGetRequestStreamBeginGetResponseGetResponseGetRequestStreamInvalidOperationException

適用於

另請參閱