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
6
  • What have you got so far? Commented Apr 26, 2013 at 14:28
  • show your code please. Commented Apr 26, 2013 at 19:32
  • 1
    You can use the OnReadyStateChange event, but it's not easy. stackoverflow.com/questions/11607677/… Commented Apr 26, 2013 at 20:20
  • @user2324106: if you show us your code we can help you to improve it Commented Apr 29, 2013 at 14:06
  • @DickKusleika Thank you for the link. How do I know which amount of data is already posted (in order to show a progress bar) ? Commented May 13, 2013 at 10:17

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

1 Comment

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?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.