2013-04-26 143 views
0

我需要從Excel工作表中將一些數據發佈到使用VBA的HTTP Web服務。 我正在使用MSXML2.XMLHTTPServer。如何跟蹤上傳進度以向用戶提供反饋(例如進度條)?使用vba跟蹤http發佈進度

這裏是我使用的代碼:我解決了通過進行猜測,是服務器的響應將被命名爲這一個大約一年前

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

你有什麼這麼遠嗎? – Tim 2013-04-26 14:28:37

+0

請顯示您的代碼。 – 2013-04-26 19:32:09

+1

您可以使用OnReadyStateChange事件,但這並不容易。 http://stackoverflow.com/questions/11607677/how-to-vba-catch-request-timeout-error – 2013-04-26 20:20:26

回答

1

,「帶的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 
+0

如果bAsync設置爲false,那麼這並不意味着'.Send pvToByteArray(sPostData)'將會阻塞,直到收到整個響應爲止。如何在收到回覆的同時獲得進展? – garbb 2017-11-21 17:54:20