FtpWebResponse.ContentLength Proprietà
Definizione
Importante
Alcune informazioni sono relative alla release non definitiva del prodotto, che potrebbe subire modifiche significative prima della release definitiva. Microsoft non riconosce alcuna garanzia, espressa o implicita, in merito alle informazioni qui fornite.
Ottiene la lunghezza dei dati ricevuti dal server FTP.
public:
virtual property long ContentLength { long get(); };
public override long ContentLength { get; }
member this.ContentLength : int64
Public Overrides ReadOnly Property ContentLength As Long
Valore della proprietà
Valore Int64 che contiene il numero di byte di dati ricevuti dal server FTP.
Esempio
Nell'esempio di codice seguente viene scaricato un file da nel server FTP specificato. Questa proprietà contiene il numero di byte nel file scaricato.
public static bool DownloadFileFromServer(Uri serverUri, string localFileName)
{
// The serverUri parameter should start with the ftp:// scheme.
if (serverUri.Scheme != Uri.UriSchemeFtp)
{
return false;
}
// Get the object used to communicate with the server.
// Note that the cast to FtpWebRequest is done only
// for the purposes of illustration. If your application
// does not set any properties other than those defined in the
// System.Net.WebRequest class, you can use the following line instead:
// WebRequest request = WebRequest.Create(serverUri);
//
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(serverUri);
request.Method = WebRequestMethods.Ftp.DownloadFile;
FtpWebResponse response = (FtpWebResponse) request.GetResponse();
Stream responseStream = null;
StreamReader readStream = null;
StreamWriter writeStream = null;
try
{
responseStream = response.GetResponseStream();
readStream = new StreamReader(responseStream, System.Text.Encoding.UTF8);
// Display information about the data received from the server.
Console.WriteLine("Bytes received: {0}",response.ContentLength);
Console.WriteLine("Message from server: {0}", response.StatusDescription);
Console.WriteLine("Resource: {0}", response.ResponseUri);
// Write the bytes received from the server to the local file.
if (readStream != null)
{
writeStream = new StreamWriter(localFileName, false);
writeStream.Write(readStream.ReadToEnd());
}
}
finally
{
if (readStream != null)
{
readStream.Close();
}
if (response != null)
{
response.Close();
}
if (writeStream != null)
{
writeStream.Close();
}
}
return true;
}
Commenti
Quando un flusso di risposta viene restituito dal server FTP, la ContentLength proprietà contiene il numero di byte nel flusso. ContentLength restituisce −1 se nella risposta non sono stati restituiti dati o se il server non ha inviato informazioni sulla lunghezza del contenuto. Il valore restituito è maggiore o uguale a zero se i dati sono o devono essere stati restituiti. Ad esempio, per le richieste che utilizzano il ListDirectory campo , la ContentLength proprietà restituisce sempre −1. Per le richieste che usano il UploadFile metodo , la ContentLength proprietà è sempre zero. Per le richieste che usano il DownloadFile metodo , la proprietà è maggiore di zero se il file scaricato contiene dati e è zero se è vuoto.
Per le richieste che utilizzano il GetFileSize metodo , ContentLength restituisce le dimensioni del file specificato nel server.