0

我需要使用 VBA 将 Excel 工作表中的一些数据发布到 HTTP Web 服务。我正在使用 MSXML2.XMLHTTPServer。如何跟踪上传进度以便向用户提供反馈(例如进度条)?

这是我使用的代码:

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
4

1 回答 1

0

大约一年前,我通过猜测服务器响应的名称来解决了这个问题,在“With CreateObject("Microsoft.XMLHTTP")”部分中,我将“.ResponseText”传递给了一个变量作为总猜测,并且在上传到 Box.com 时有效!这是下面代码中的最后一行,取自:http ://wqweto.wordpress.com/2011/07/12/vb6-using-wininet-to-post-binary-file/

注意,bAsync 变量必须设置为 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
于 2014-11-05T15:58:45.213 回答