FtpWebRequest.ContentOffset Właściwość

Definicja

Pobiera lub ustawia przesunięcie bajtów do pliku pobieranego przez to żądanie.

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

Wartość właściwości

Wystąpienie Int64 określające przesunięcie pliku w bajtach. Wartość domyślna to zero.

Wyjątki

Dla tej właściwości określono nową wartość dla żądania, które jest już w toku.

Wartość określona dla tej właściwości jest mniejsza niż zero.

Przykłady

Poniższy przykład kodu przedstawia pobieranie części pliku z serwera i dołączanie pobranych danych do pliku lokalnego.

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;
}

Uwagi

ContentOffset Ustaw właściwość podczas pobierania pliku z serwera FTP. To przesunięcie wskazuje pozycję w pliku serwera, który oznacza początek pobierania danych. Przesunięcie jest określane jako liczba bajtów od początku pliku; przesunięcie pierwszego bajtu wynosi zero.

Ustawienie ContentOffset powoduje FtpWebRequest wysłanie polecenia ponownego uruchomienia (REST) do serwera. To polecenie jest ignorowane przez większość implementacji serwera FTP, jeśli przekazujesz dane na serwer.

Zmiana ContentOffset po wywołaniu GetRequestStreammetody , BeginGetRequestStream, GetResponselub BeginGetResponse powoduje InvalidOperationException wyjątek.

Dotyczy

Zobacz też