GetFileSize Method  
 

Return the size of the specified file on the server.

Syntax

object.GetFileSize( RemoteFile, FileSize )

Parameters

RemoteFile
A string that specifies the name of the file on the server. The filename cannot contain any wildcard characters and must follow the naming conventions of the operating system the server is hosted on.
FileSize
A numeric variable which will be set to the size of the file on the server. Note that if the variable is not large enough to contain the file size, an overflow error will occur. This parameter must be passed by reference.

Return Value

A value of zero is returned if the operation was successful, otherwise a non-zero error code is returned which indicates the cause of the failure.

Remarks

The GetFileSize method uses the HEAD command to retrieve header information about the file without downloading the contents of the file itself. This requires that the server support at least version 1.0 of the protocol standard, or an error will be returned.

The server may not return a file size for some resources. This is typically the case with scripts that generate dynamic content because the server has no way of determining the size of the output generated by the script without actually executing it. The server may also not provide a file size for HTML documents which use server side includes (SSI) because that content is also dynamically created by the server. If the request to the server was successful and the file exists, but the server does not return a file size, the method will succeed but the file size returned to the caller will be zero.

When a request is made to the server for information about the file, the control will attempt to keep the connection alive, even if the KeepAlive property has not been set to True. This allows an application to request the file size and then download the file without having to write additional code to re-establish the connection. However, it is possible that the attempt to keep the connection open will fail. In that case, an error will be returned and the session will no longer be valid. If this happens, the method may still return a valid file size. To determine if an error occurred, check the value of the LastError property.

Note that if the file on the server is a text file, it is possible that the value returned by this method will not match the size of the file when it is downloaded to the local system. This is because different operating systems use different sequences of characters to mark the end of a line of text, and when a file is transferred in text mode, the end of line character sequence is automatically converted to a carriage return-linefeed, which is the convention used by the Windows platform.

Example

The following example demonstrates how to retrieve the size a file on the server:

Dim nFileSize As Long

nError = HttpClient1.GetFileSize(strFileName, nFileSize)
If nError > 0 Then
    MsgBox HttpClient1.LastErrorString, vbExclamation
    Exit Sub
End If

MsgBox "The size of " & strFileName & " is " & nFileSize " bytes"

See Also

GetFileTime Method