0

I need to post some data from an Excel worksheet to an HTTP web service with VBA. I'm using MSXML2.XMLHTTPServer. How can I track the upload progress in order to give a feedback to the user (e.g. a progress bar)?

Here is the code I use :

Const STR_BOUNDARY  As String = "3fbd04f5-b1ed-4060-99b9-fca7ff59c113"

     '--- prepare body
    PostData = "--" & STR_BOUNDARY & vbCrLf & _
        "Content-Disposition: form-data; name=""path""; filename=""" & fileName & """" & vbCrLf & _
        "Content-Type: application/octet-stream" & vbCrLf & vbCrLf & _
        PostData & vbCrLf & _
        "--" & STR_BOUNDARY & "--"
    '--- post
        objHTTP.Open "POST", Url, False
        objHTTP.SetRequestHeader "Content-Type", "multipart/form-data; boundary=" & STR_BOUNDARY
        objHTTP.Send pvToByteArray(PostData)

    PostString = objHTTP.responseText
bbo
  • 13
  • 4

1 Answers1

1

I solved this one about a year ago by making a guess as to what the server response would be named, within the "With CreateObject("Microsoft.XMLHTTP")" part, I passed ".ResponseText" to a variable as a total guess, and it worked when uploading to Box.com! It's the last line in the code below which was taken from: http://wqweto.wordpress.com/2011/07/12/vb6-using-wininet-to-post-binary-file/

Note, the bAsync Variable must be set to False

'--- read file
nFile = FreeFile
Open sFileName For Binary Access Read As nFile
If LOF(nFile) > 0 Then
    ReDim baBuffer(0 To LOF(nFile) - 1) As Byte
    Get nFile, , baBuffer
    sPostData = StrConv(baBuffer, vbUnicode)
End If
Close nFile
'--- prepare body
    sPostData = "--" & STR_BOUNDARY & vbCrLf & _
        "Content-Disposition: form-data; name=""uploadfile""; filename=""" & Mid$(sFileName, InStrRev(sFileName, "\") + 1) & """" & vbCrLf & _
        "Content-Type: application/octet-stream" & vbCrLf & vbCrLf & _
        sPostData & vbCrLf & _
        "--" & STR_BOUNDARY & "--"
'--- post
    Dim sResponseText As String
    With CreateObject("Microsoft.XMLHTTP")
        .Open "POST", sURL, bAsync
        .SetRequestHeader "Content-Type", "multipart/form-data; boundary=" & STR_BOUNDARY
        .Send pvToByteArray(sPostData)
        sResponseText = .ResponseText
    End With
cows2
  • 19
  • 2
  • If bAsync is set to false then doesn't that mean that `.Send pvToByteArray(sPostData)` will block until the entire response is received? How do you get the progress while the response is being received? – garbb Nov 21 '17 at 17:54