When we perform a directory FTP Execute operation such as DIR, LS, or PWD, the data is not returned to us immediately; instead, it is stored in a buffer and we must use the GetChunk() (as in "get a chunk of data") method to read it.
Let's say we performed a DIR operation with the Execute() method. In that case, we use GetChunk() to retrieve the directory data we received. We use GetChunk() like this: Inet1.GetChunk(size, type). Here, size is the number of bytes we want to get from the download buffer, and type can be one of two constants: icString (value = 0, the default) for string data, or icByteArray (value = 1) for binary data. Our goal might be to display the directory data we got in a text box named Text1. We would start in the Internet Transfer control's StateChanged event handler. We wait until the directory data has been downloaded by checking the State variable and making sure it equals icResponseReceived:
Private Sub Inet1_StateChanged(ByVal State As Integer)
—> If(State = icResponseReceived) Then 'This constant = 12
.
.
.
—> End If
End Sub
If we got the directory data, we can begin by getting, say, 2K of data from the download buffer:
Private Sub Inet1_StateChanged(ByVal State As Integer)
If(State = icResponseReceived) Then 'This constant = 12
—> Dim VariantData As Variant
—> VariantData = Inet1.GetChunk(2048, icString)
.
.
.
End If
End Sub
Note that GetChunk() returns a Variant: an untyped value that can be either a string or numeric data. In this case, we'll keep reading from the download buffer until we exhaust it, in which case GetChunk will return an empty string, "":
Private Sub Inet1_StateChanged(ByVal State As Integer)
If(State = icResponseReceived) Then 'This constant = 12
Dim VariantData As Variant
—> Dim StringData As String
—> Dim DoneFlag As Boolean
—> DoneFlag = False
—> StringData = ""
VariantData = Inet1.GetChunk(2048, icString)
—> While Not DoneFlag
—> StringData = Data + VariantData
—> VariantData = Inet1.GetChunk(2048, icString)
—> If Len(VariantData) = 0 Then DoneFlag = True
—> Wend
.
.
.
End If
End Sub
At this point, the directory data we received is in the string StringData, and we can display it in our text box Text1 this way:
Private Sub Inet1_StateChanged(ByVal State As Integer)
If(State = icResponseReceived) Then 'This constant = 12
Dim VariantData As Variant
Dim StringData As String
Dim DoneFlag As Boolean
DoneFlag = False
StringData = ""
VariantData = Inet1.GetChunk(2048, icString)
While Not DoneFlag
StringData = Data + VariantData
VariantData = Inet1.GetChunk(2048, icString)
If Len(VariantData) = 0 Then DoneFlag = True
Wend
—> Text1.Text = StringData
End If
End Sub