Chilkat HOME ASP Visual Basic VB.NET C# Visual C++ C MFC Delphi FoxPro Java Perl PHP Python Ruby SQL Server VBScript
|
Asynchronous Upload in a Background ThreadVisual Basic 6.0 example to do an HTTP upload asynchronously in a background thread. Dim abortUpload As Long
' Called when the app's "Abort Upload" button is clicked.
Private Sub AbortButton_Click()
abortUpload = 1
End Sub
Private Sub AsyncUpload_Click()
abortUpload = 0
Dim upload2 As ChilkatUpload
' Create an instance of the Chilkat Upload component.
' This is a free component.
Set upload2 = New ChilkatUpload
' Set our idle timeout to 30 seconds (30000 millisec).
' If progress halts for more than this time, the upload
' is automatically aborted.
upload2.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.
upload2.ChunkSize = 2048
' Set the target for the upload.
' This example tests against http://freeaspupload.net
upload2.HostName = "www.freeaspupload.net"
upload2.Port = 80
upload2.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.
upload2.AddFileReference "attach1", "c:\temp\hamlet.xml"
' Multiple files may be uploaded at once by calling
' AddFileReference multiple times.
' upload2.AddFileReference "attach2", "c:\temp\a\dudeA.gif"
' upload2.AddFileReference "attach3", "c:\temp\a\hello.txt"
' upload2.AddFileReference "attach4", "c:\temp\hamlet.xml"
' Start an asynchronous upload in a background thread.
upload2.BeginUpload
' Wait for the upload to complete. Your application might do other
' useful tasks rather than simply waiting...
Do
' Sleep a short time (.1 seconds)
upload2.SleepMs 100
DoEvents
' Update our progress bar with the percentage complete
ProgressBar1.Value = upload2.PercentUploaded
' If this app's abort button was pressed, abort the upload:
If (abortUpload = 1) Then
upload2.abortUpload
End If
Loop Until upload2.UploadInProgress = 0
If (upload2.UploadSuccess = 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 (upload2.ResponseStatus <> 200) Then
upload2.SaveLastError "uploadError.xml"
MsgBox "Failed to upload, HTTP response code = " + Str(upload2.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 = upload2.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 = upload2.VariantToString(upload2.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 upload2.LastErrorText
' Error information may also be saved to a file:
upload2.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.