Visit us at https://www.vbparadise.com. In this tutorial I will cover how to perform file transfers between your PC and a web server. The topics of web site management, dynamic generation of web pages, and control/script inclusion in web pages is covered in a different tutorial.
In the Professional and Enterprise Editions of VB are the two controls which provide Internet features - the Internet Transfer Control and the Web Browser Control. The Internet Transfer Control (ITC) provides both FTP and HTTP file transfer controls and is the subject of this tutorial - as is the use of the wininet.dll, which also provides file transfer capabilities that you can access from within your VB applications.
One of the key drawbacks of using the ITC for file transfer (regardless of the protocol that is used) is that it does not provide any built-in capability to identify how many bytes of the transfer are complete at any point in time. All you can tell from within your VB program is whether the file transfer is in progress, or that it has stopped (because of a successful transfer or some error that stopped the file transfer process). This is one of the key reasons you might be interested in looking at one of the 3rd party file transfer OCXs.
OpenURL Method of File Transfer
Now that I've gotten the introductory comments out of the way, let's talk about the details of the ITC. The two most important things to know about the ITC is that there are two methods of downloading files from a web site - the OpenURL method and the Execute method. Both support the FTP and HTTP file transfer protocols.
The OpenURL method is
very simple. You put in a file name to download and tell the program whether the
file is all text or binary. The code looks for an HTTP transfer of a text file
looks like this:
text1.text = inet1.OpenURL ("http://www.vbinformation.com/badclick.htm", icString)
The code for an HTTP transfer of a binary file
looks like this:
Dim bData() as Byte
bData() = inet1.OpenURL ("http://www.vbinformation.com/badclick.htm", icByteArray)
Since all files (text or binary) can be transferred as a binary file, I used the same file name in both examples. Note that in the first case, the downloaded file content is placed in a textbox named 'text1'. In the second case, the downloaded file content is saved in a Byte array whose upper bound is set by the number of bytes downloaded by the OpenURL method. Also, note that both examples use HTTP URLs, but FTP URLs could have been used just as readily.
In case you
don't remember, an easy way to save the bData byte array is:
Open "filename" for Binary as #1 Put #1, , bData() Close #1
This is really all there is to successfully downloading a file by using the OpenURL method. I'll cover the question of errors (such as when the server is down, or the file is not there) later in this tutorial.
You should note that the OpenURL method is synchronous - which simply means that any code that follows the OpenURL statement will not be executed until the file transfer is completed, or until the file transfer is stopped by the occurence of an error or by a user command (I'll show how to do this later).
Execute Method of File Transfer
The second method for downloading a file is the Execute method. As you'll see it provides more features, but is definitely more complicated to code. The one key difference that you'll want to be aware of is that with the Execute method the bytes of data are sometimes, but not always, kept within the ITC itself (in a memory buffer). When the ITC does keep the downloaded bytes in its buffer, you must use another method called GetChunk to extract the dowloaded bytes. Whether the memory buffer is used varies with the arguments used in calling the Execute method. I'll give more detail on that later.
Another key difference that you should know about is the Execute method is asynchronous - meaning that it will download the file in the background and that any code following the Execute statement will be executed immediately.
Finally, to complicate the discussion a bit more, the arguments you use in the Execute method differ depending on whether you want to use FTP or HTTP for the file transfer.
Here's the general syntax for
the Execute method:
inet1.Execute (url, operation, data, requestheaders)
For an FTP file transfer, only the first two arguments are used. For an HTTP file transfer, all four arguments may be used.
Here's an example of the code you would
use to start a transfer using the Execute method and the FTP protocol:
inet1.Execute ("ftp://www.microsoft.com", "DIR")
This command transfers the directory listing of the Microsoft ftp site. Note than while the OpenURL method returns data to a variable or an array, the Execute method does not! The data returned by the Execute method will either be kept within the ITC's buffer, or be directed to a file according to the specifics of the command it is given.
The Execute method actually supports 14 FTP
commands (which are placed in the 'operation' argument), but there are primarily
three (CD, GET, and PUT) which you will use most often:
The first of these three allow you to make the connection to the FTP server and to navigate to the directory where the files are located. The second shows how to GET a file from the server and put it on your PC, while the third shows how to PUT a local file from your PC onto the FTP server (in the directory to which you navigated to using the CD command).
Also, you will note that the GET and PUT commands create a file on either the local or remote computers. In these cases, the ITC memory buffer is not used. However, the ITC memory buffer must be accessed in order to get the output of the 'DIR' command.
In order to discuss how the ITC memory buffer is accessed, we have to talk first about the StateChanged Event. Statechanged is the only event the ITC control has, and it provides a variable called 'State' which must be read to determine the status of a pending Execute method.
The State values are:
Typically, Select Case code is used within the StateChanged event to determine what action to take. In general, only actions 8, 11, and 12 are used to generate a code response. The others are used mostly to decide what message to display in a status label/toolbar.
For State=12, where the file transfer is complete, the action you take is entirely up to you. This would usually be a simple popup message telling the user that the file transfer is complete.
For State=11, which indicates that an error has occurred, you would have to generate code necessary to correct or ignore the error condition.
Generally, you simply wait for State=12 to indicate that the transfer is complete. But, in some cases you may want to begin extracting data before the transfer is complete. For example, the HTTP header information is received first, but is not included in the ITC download buffer. To get that information you use the .GetHeader method. You can use the State=8 to determine when the header information is available.
In those cases where the ITC buffer is
used to temporarily store the downloaded information, the .GetChunk method is
used. Here's the code for the case where string data is being downloaded and a
State=12 has been received to indicate that the transfer is complete:
Do DoEvents bData = inet1.GetChunk (1024, icString) AllData = AllData & bData Loop Until bData = ""
In the case where a State=8 has been received, it is possible that no actual data is in the ITC buffer (such as when only header information has been received). So, if the above code is used following a State=8 event, the condition of bData="" may not indicate completion of the data transfer.
Finally, remember that you may have to set the .UserName and .Password properties if you are using the FTP commands within the Execute method. If the FTP site you are accessing allows 'anonymous' logon's, then you will not have to set these properties.
As you saw in the sample code, the basics of file transfer don't require knowledge of all 19 properties, 5 methods, and one event exposed by the ITC. The following table list all of the ITC interface elements, but as you will see in the discussion that follows, you will not use but a few of these in most applications:
| Properties | Methods | Events |
|---|---|---|
| AccessType | Cancel | StateChanged |
| Document | Execute | |
| hInternet | GetChunk | |
| Password | GetHeader | |
| Protocol | OpenURL | |
| Proxy | ||
| RemoteHost | ||
| RequestTimeout | ||
| ResponseCode | ||
| ResponseInfo | ||
| StillExcuting | ||
| URL | ||
| UserName |
In addition, the normal control properties of .Index, .Left, .Top, .Tag, .Parent and .hInternet are available for the ITC.
This table can be digested more easily if you think in terms of how the properties/methods/events are used. Here's the way I've grouped them in my notes:
The bottom line of this tutorial section is that file transfers can be made very easily using the ITC and just a handful of the properties, methods and events supported by the control.
As a final note, in case you've been watching carefully you will notice that I left out any discussion on the use of the .Execute method with HTTP commands. This was strictly for lack of time/space in this tutorial. The .Execute method can be used equally with either FTP or HTTP commands, but the FTP options are generally more extensive so FTP is the normal choice for programmers.
--- info on using wininet.dll goes here ----