Visual Basic Examples

ChilkatHOMEASPVisual BasicVB.NETC#Visual C++CMFCDelphiFoxProJavaPerlPHPPythonRubySQL ServerVBScript

VB Examples

Bounced Mail
Character Encoding
Digital Certificates
Digital Signatures
Email
FTP
HTML-to-XML
HTTP
IMAP
Encryption
MHT / HTML Email
RSA Encryption
S/MIME
Socket
Spider
String
Tar
Unicode
Upload
XML
XMP
Zip Compression

More Examples...
Email Object
POP3
SMTP
RSS
Atom
Self-Extractor


VB Strings
VB Byte Array

Unreleased...
Service
PPMD
Deflate
Bzip2
LZW
Bz2
DH Key Exchange
DSA
Icon

 

 

 

 

 

 

 

Upload Files via HTTP

This Visual Basic 6.0 example demonstrates how to do HTTP uploads with progress monitoring event callbacks and abort capability. This Chilkat Upload ActiveX component is freeware.

Dim WithEvents upload As ChilkatUpload
Dim abortUpload As Long

' Called when the app's "Abort Upload" button is clicked.
Private Sub AbortButton_Click()
    abortUpload = 1
End Sub

' Called every ChilkatUpload.HeartbeatMs milliseconds.
Private Sub upload_Heartbeat(abort As Long)
    List1.AddItem "heartbeat"
    DoEvents
    
    ' To abort the upload, set abort = 1
    abort = abortUpload
End Sub

' Called each time the percentage completion increases.
Private Sub upload_PercentDone(ByVal PercentDone As Long, abort As Long)
    ProgressBar1.Value = PercentDone
    
    ' To abort the upload, set abort = 1
    abort = abortUpload
End Sub

Private Sub UploadButton_Click()

    abortUpload = 0
    
    ' Create an instance of the Chilkat Upload component.
    ' This is a free component.
    Set upload = New ChilkatUpload
    
    ' Set our heartbeat to 200 milliseconds.  The heartbeat
    ' event is periodically called while the upload is in
    ' progress.
    upload.HeartbeatMs = 200
    
    ' Set our idle timeout to 30 seconds (30000 millisec).
    ' If progress halts for more than this time, the upload
    ' is automatically aborted.
    upload.IdleTimeoutMs = 30000
    
    ' Set the ChunkSize.  The ChunkSize can help with performance.
    ' It is the size, in bytes, of each TCP socket write during
    ' the upload.
    upload.ChunkSize = 2048
    
    ' Set the target for the upload.
    ' This example tests against http://freeaspupload.net
    upload.HostName = "www.freeaspupload.net"
    upload.Port = 80
    upload.Path = "/freeaspupload/testUpload.asp"
    
    ' Add some files to be uploaded...
    
    ' The AddFileReference method does not read the files into
    ' memory.  It simply adds a reference within the ChilkatUpload
    ' object so that the file is included when upload is initiated.
    
    ' The 1st argument is the name that would be in an HTML input
    ' tag, such as this:
    ' <input name=attach1 type=file size=20>
    
    ' The 2nd argument is an existing file on disk.  The filepath,
    ' if included, may be absolute or relative to the current working
    ' directory.
    upload.AddFileReference "attach1", "c:\temp\hamlet.xml"
    
    ' Multiple files may be uploaded at once by calling
    ' AddFileReference multiple times.
    ' upload.AddFileReference "attach2", "c:\temp\a\dudeA.gif"
    ' upload.AddFileReference "attach3", "c:\temp\a\hello.txt"
    ' upload.AddFileReference "attach4", "c:\temp\hamlet.xml"
    
    ' In this case, we will do a blocking upload because there are
    ' events to track progress and abort if necessary.
    ' It is also possible to upload asynchronously in the background,
    ' but that is left for another example...
    success = upload.BlockingUpload()
    
    If (success = 1) Then
        ' The upload was a success in that the HTTP upload was sent
        ' and we received an HTTP response.  You may wish to check
        ' the HTTP response to ensure that it is what you expect.
        
        ' The HTTP response status code (i.e. 200, 404, etc.) is available
        ' in the ResponseStatus property:
        If (upload.ResponseStatus <> 200) Then
        
            upload.SaveLastError "uploadError.xml"
            
            MsgBox "Failed to upload, HTTP response code = " + Str(upload.ResponseStatus)
            
        Else
            ' We have a valid 200 response.
            ' The response header and body are available in the ResponseHeader
            ' and ResponseBody properties.  ResponseHeader is a string property.
            ' However, ResponseBody is a Variant.  We'll need to convert that to a string.
            
            ' Display the response header:
            Text1.Text = upload.ResponseHeader
            
            ' Get the response body as a string.  In this case, the response
            ' is known to be HTML in the iso-8859-1 encoding:
            Dim html As String
            html = upload.VariantToString(upload.ResponseBody, "iso-8859-1")
            Text2.Text = html

            ' If the upload succeeded, we will find this string in the HTML:
            If (InStr(html, "Upload completed") > 0) Then
                MsgBox "Upload complete!"
            Else
                ' An unexpected response was received.  Display the HTML source:
                MsgBox "Unexpected HTML response!"
            End If
            
            
        End If
        
    Else
        ' The upload failed.  Error information is available in
        ' LastErrorText, LastErrorXml, or LastErrorHtml.
        MsgBox upload.LastErrorText
        
        ' Error information may also be saved to a file:
        upload.SaveLastError "uploadError.xml"
    End If
    
End Sub

 

Need a specific example? Send a request to support@chilkatsoft.com

© 2000-2008 Chilkat Software, Inc. All Rights Reserved.