FtpWebRequest.ContentOffset 属性

定义

获取或设置请求所下载的文件的字节偏移量。Gets or sets a byte offset into the file being downloaded by this request.

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

指定文件偏移量(以字节为单位)的 Int64 实例。An Int64 instance that specifies the file offset, in bytes. 默认值为零。The default value is zero.

例外

对于一个已在进行的请求为此属性指定了一个新值。A new value was specified for this property for a request that is already in progress.

为该属性指定的值小于 0。The value specified for this property is less than zero.

示例

下面的代码示例演示如何从服务器下载部分文件,并将下载的数据追加到本地文件。The following code example demonstrates downloading part of a file from a server and appending the downloaded data to a local file.

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 服务器下载文件时设置属性。Set the ContentOffset property when downloading a file from an FTP server. 此偏移量指示服务器文件中用于标记要下载的数据开始位置的位置。This offset indicates the position in the server's file that marks the start of the data to be downloaded. 偏移量指定为从文件开头开始的字节数;第一个字节的偏移量为零。The offset is specified as the number of bytes from the start of the file; the offset of the first byte is zero.

设置 ContentOffset 会导致将 FtpWebRequest 重新启动 (REST) 命令发送到服务器。Setting ContentOffset causes the FtpWebRequest to send a restart (REST) command to the server. 如果要将数据上传到服务器,则多数 FTP 服务器实现将忽略此命令。This command is ignored by most FTP server implementations if you are uploading data to the server.

ContentOffset在调用 GetRequestStreamBeginGetRequestStream 、或方法之后更改将 GetResponse BeginGetResponse 导致 InvalidOperationException 异常。Changing ContentOffset after calling the GetRequestStream, BeginGetRequestStream, GetResponse, or BeginGetResponse method causes an InvalidOperationException exception.

适用于

另请参阅