restuploadvb6

How to get progress of file upload in percentage


I have prepared a code sample that allows me to send a series of parameters along with a file to an API address like https://aaa.com/api/token/sendfile. I have used MSXML2.XMLHTTP in the project, but I still don't know how to get the percentage of the file being uploaded. Does anyone have an example? (It's OK if it works with winhttp or other ways, too.)

Here is simple code to explain what I am trying to do:

Dim RetVal As Object
Set RetVal = CreateObject("MSXML2.XMLHTTP")
RetVal.Open "POST", "https://aaa.com/api/token/sendfile", false
RetVal.SetRequestHeader "User-Agent", "aaa"
v.SetRequestHeader "Content-Type", "multipart/form-data; charset=utf-8; boundary=" & STR_BOUNDARY
RetVal.SetRequestHeader "Content-Length", xsize
RetVal.Send custombytedata
vResult = RetVal.ResponseText

Solution

  • I solved the problem with Chilkat.

    Dim WithEvents rest As ChilkatRest
    
    Private Sub Command1_Click()
    
        Set rest = New ChilkatRest
        Dim bTls As Long
        bTls = 1
        Dim port As Long
        port = 443
        Dim bAutoReconnect As Long
        bAutoReconnect = 1
        Dim success As Long
        rest.PercentDoneOnSend = 1
        success = rest.Connect("www.aaa.com", port, bTls, bAutoReconnect)
        If (success <> 1) Then
            MsgBox rest.LastErrorText
            Exit Sub
        End If
        
        Dim fileStream As New ChilkatStream
        fileStream.SourceFile = "1.jpg"
        
        success = rest.AddHeader("User-Agent", "aaaa")
        success = rest.AddHeader("Content-Type", "multipart/form-data;charset=UTF-8")
    
        rest.PartSelector = "1"
        success = rest.AddHeader("Content-Disposition", "form-data; name=""date""")
        success = rest.SetMultipartBodyString("0")
        
        rest.PartSelector = "2"
        success = rest.AddHeader("Content-Disposition", "form-data; name=""chat_id""")
        success = rest.SetMultipartBodyString("123456")
        
        rest.PartSelector = "3"
        success = rest.AddHeader("Content-Disposition", "form-data; name=""caption""")
        success = rest.SetMultipartBodyString("عنوان")
        
        
        rest.PartSelector = "4"
        success = rest.AddHeader("Content-Type", "application/octet-stream")
        success = rest.AddHeader("Content-Disposition", "form-data; name=""file""; filename=""1.jpg""")
        success = rest.SetMultipartBodyStream(fileStream)
        rest.PartSelector = "0"
        
        
    
        Dim responseBody As String
        responseBody = rest.FullRequestMultipart("POST", "/api/token/sendFile")
        
        
        If (rest.LastMethodSuccess <> 1) Then
            MsgBox rest.LastErrorText
            Exit Sub
        End If
        
        If (rest.ResponseStatusCode <> 200) Then
            Debug.Print "Received error response code: " & rest.ResponseStatusCode
            Exit Sub
        End If
        
        Caption = "File uploaded"
        
        'MsgBox responseBody
        
        Dim jResp As New ChilkatJsonObject
        success = jResp.Load(responseBody)
        jResp.EmitCompact = 0
        MsgBox jResp.Emit()
        ' example output
        MsgBox jResp.StringOf("ok")
        MsgBox jResp.StringOf("result.date")
        MsgBox jResp.StringOf("result.caption")
    
    End Sub
    
    
    
    Private Sub rest_AbortCheck(abort As Long)
        Debug.Print "Abortcheck : " & abort
    End Sub
    
    Private Sub rest_BinaryData(ByVal data As Variant)
        Debug.Print " rest_BinaryData : " & data
    End Sub
    
    Private Sub rest_PercentDone(ByVal percent As Long, abort As Long)
        Caption = CStr(percent) & " - " & CStr(abort)
    End Sub
    
    Private Sub rest_ProgressInfo(ByVal name As String, ByVal value As String)
        Debug.Print name & " : " & value
    End Sub
    
    Private Sub rest_TextData(ByVal data As String)
        Debug.Print " rest_textdata : " & data
    End Sub