FtpWebRequest.ContentOffset Property
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
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
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.
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;
}
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.
Product | Versions |
---|---|
.NET | Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9 |
.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 |
.NET feedback
.NET is an open source project. Select a link to provide feedback: