我需要将一些数据从Excel工作表发布到带有VBA的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
答案 0 :(得分:1)
我在大约一年前通过猜测服务器响应的名称来解决这个问题,在#34;使用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