Export (0) Print
Expand All

FtpWebRequest.ContentOffset Property

Gets or sets a byte offset into the file being downloaded by this request.

Namespace:  System.Net
Assembly:  System (in System.dll)

'Declaration
Public Property ContentOffset As Long

Property Value

Type: System.Int64
An Int64 instance that specifies the file offset, in bytes. The default value is zero.

ExceptionCondition
InvalidOperationException

A new value was specified for this property for a request that is already in progress.

ArgumentOutOfRangeException

The value specified for this property is less than zero.

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.

Setting ContentOffset causes the FtpWebRequest to send a restart (REST) command to the server. This command is ignored by most FTP server implementations if you are uploading data to the server.

Changing ContentOffset after calling the GetRequestStream, BeginGetRequestStream, GetResponse, or BeginGetResponse method causes an InvalidOperationException exception.

The following code example demonstrates downloading part of a file from a server and appending the downloaded data to a local file.


Public Shared Function RestartDownloadFromServer(ByVal fileName As String, ByVal serverUri As Uri, ByVal offset As Long) As Boolean
    ' 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 Then
        Return False
    End If
    ' Get the object used to communicate with the server.
    Dim request As FtpWebRequest = CType(WebRequest.Create(serverUri), FtpWebRequest)
    request.Method = WebRequestMethods.Ftp.DownloadFile
    request.ContentOffset = offset
    Dim response As FtpWebResponse = Nothing
    Try
        response = CType(request.GetResponse(), FtpWebResponse)
    Catch e As WebException
        Console.WriteLine(e.Status)
        Console.WriteLine(e.Message)
        Return False
    End Try
    ' Get the data stream from the response.
    Dim newFile As Stream = response.GetResponseStream()
    ' Use a StreamReader to simplify reading the response data.
    Dim reader As New StreamReader(newFile)
    Dim newFileData As String = reader.ReadToEnd()
    ' Append the response data to the local file
    ' using a StreamWriter.
    Dim writer As StreamWriter = 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
End Function


.NET Framework

Supported in: 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Community Additions

ADD
Show:
© 2014 Microsoft