POSTing Form Data to a Web Page

Posting a File

As we mentioned earlier, it is possible to emulate the <INPUT type=file> tag using the WinINet API functions. For the most part, the code from the basic example remains the same. The difference is in the header that is included with the request and the format of the data itself.

The header is relatively straightforward. Instead of form encoding the data, the content type shown below indicates that the data can be found in multiple places in the request body. Now the web server will expect blocks of data with some type of boundary marker to separate each one.

sHeader = "Content-Type: multipart/form-data; boundary=913114112" & vbCrLf

The value of the boundary property is used to identify the start and end of the various blocks of data in the request. You can see how it is used in the code sample below. As a quite note to keep you from wasting your time, the dashes before and after the boundary value, as well as the positioning of the vbNewLine variables is critical to the successful functioning of this process. Eight hours of debugging while trying to come up with the correct combination is proof of that.

lpszPostData = "--913114112" & vbNewLine & "Content-Disposition:" & _
    "multipart/form-data; name=""filePath""" & vbNewLine & vbNewLine & _
    "This is a test of the input file post" & vbNewLine & "--913114112--"
lPostDataLen = Len(lpszPostData)

The actual value of the bounday property doesn't make any difference. Just so that you don't find it elsewhere in the request. The name of the input element is defined in the name property. This can be used on the server side (in TestPost.asp, for example) to access the data. Also, if you plan on posting binary data to the form, run the data through the Base64 algorithm. This converts the untransmitable characters to a more verbose, but Internet-friendly sequence of characters.

You might also like...

Comments

About the author

Bruce Johnson Canada

I am the owner of a small application development consulting company that specialized in the design and implementation of Internet-based applications. While there are others who can make a web ...

Interested in writing for us? Find out more.

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“To iterate is human, to recurse divine” - L. Peter Deutsch