Using the Internet Transfer Control: Part 3
Well, in the Inet's StateChanged event, we need to monitor when data comes in and then do some trickery to get what the server is sending. Firstly, it is a good idea to store the current command (e.g. GET) as a module level variable. This way all the procedures can access it and we know what we are currently doing.
For example, if we wanted a list of directories, then we would set our variable to DIR, and then use the Execute method. This way when we are in the StateChanged event, a completely different procedure, we still know what the last command was.
The StateChanged event procedure provides us with one parameter, State. We can use a Select Case statement to determine what is currently going on. Now, the server only sends back data after the state of icResponseCompleted, so we need to stick in another Select Case statement to find out what command we are executing.
Lets quickly recap on the code. Here is what we have so far in the StateChanged event:
Private Sub Inet1_StateChanged(ByVal State As Integer) Select Case State Case icResponseCompleted Select Case strCmd Case "DIR" End Select End Select End Sub
OK. As the StateChanged event only passes the State parameter, we need to manually get the data that is being sent by the server. We do this using the GetChunk method. Because of the way things are, we can only get 512 characters at a time, so we need to set-up a loop that will make sure that we get all the data. It looks something like this:
Do strNewData = Inet1.GetChunk(512,icString) DoEvents If Len(strData) = 0 Then Exit Do strData = strData & strNewData Loop
Notice the use of DoEvents. This is very important because we must give the server time to sort itself out otherwise we will run into a whole load of problems.
If you are currently testing out this code, you will notice that the contents of strData are not exactly 'user friendly' at the moment. We still need to go through and format the data, for whatever control we are going to load it into. Each file and directory is in the string and at the end of each one, a vbCrlf character can be found. This makes it easy to parse out all the different items using some of VBs great string functions:
Do While Len(strData) intPos = Instr(strData,vbCrlf) If intPos Then strItem = Left$(strData, intPos - 1) strData = Mid$(strData, intPos + 2) Else strItem = strData strData = "" End If Loop
But how do we know the difference between a file and a directory? Well, fortunately all directories come with a trailing /, so we can look for that when we want to do something with the item:
strItem = lstDir.List(lstDir.ListIndex) If strItem = ".." Or Right$(strItem,1) = "/" Then DoEvents If strItem = ".." Then strCmd = "CDUP" Inet1.Execute ,strCmd Else strCmd = "CD " & Left$(strItem, Len(strItem) -1) Inet1.Execute ,strCmd End If End If
OK. So now we have a list of directories. The next thing we are going to learn, is how to upload files.
Page 4 of 6
This article was originally published on November 20, 2002