FtpWebRequest.ContentOffset 屬性

定義

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

public long ContentOffset { get; set; }

屬性值

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

例外狀況

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

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

範例

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

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

適用於

產品 版本
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

另請參閱