Upload a File Code Example

The following example demonstrates a file upload using the Web control. Uploading files requires a multipart MIME message, which are created automatically by the Web control. Contrast this with the PowerTCP Http control, which requires the developer to create the MIME message manually.

 

Private Function PostFile(ByVal url As StringByVal filename As String)
    'Open file to read and post.
    Dim file As New DartStream
    file.FileMode = createExisting
    file.filename = filename
    file.Ref = "file1"
    file.Position = 0

    'Add file to files to post collection.
    Dim files As New DartStreams
    files.Add file
    Web1.Request.FilesToPost = files

    'Send Post and get server response.
    Web1.Request.url = url
    Web1.Post

    'Return response.
    Dim response As String
    response = GetVersion(Web1.response.version) + " "
    response = response + CStr(Web1.response.Status)
    response = response + " " + Web1.response.StatusText + vbCrLf
    response = response + Web1.response.Header.All + vbCrLf
    response = response + Web1.response.Body.ReadString
    PostData = response
End Function

Private Function GetVersion(ByVal version As WebVersionConstants)
    GetVersion = ""
    If version = webHTTP10 Then GetVersion = "HTTP/1.0"
    If version = webHTTP11 Then GetVersion = "HTTP/1.1"
End Function