2012-08-29 94 views
1

我在Visual Basic .NET中有此代碼,我想在Visual Basic 6中做同樣的事情。代碼將圖像上傳到給定的URL。我必須使用Rest協議,因爲服務器上傳圖像請求它。Visual Basic 6中的RestSharp

Dim res As New RestSharp.RestRequest("URL_OF_SERVER", RestSharp.Method.POST) 

res.AddFile("file", "File_location_on_PC") 
Dim restClient As New RestSharp.RestClient() 

Dim r As New RestSharp.RestResponse 
r = restClient.Execute(res) 

是否有可能在Visual Basic 6中執行相同的Rest協議?

編輯1:我有試過,但發生錯誤的時候

Dim xmlhttp As MSXML2.XMLHTTP30 
Const STR_BOUNDARY As String = "3fbd04f5-b1ed-4060-99b9-fca7ff59c113" 
Dim nFile   As Integer 
Dim baBuffer()  As Byte 
Dim sPostData  As String 

'--- read file 
nFile = FreeFile 
Open NombreArchivo 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=""image002""; filename=""C:\image002.jpg" & _ 
"--" & STR_BOUNDARY & "--" 
'--- post 
Set xmlhttp = New MSXML2.XMLHTTP30 
With xmlhttp 
    .Open "POST", Url, False 
    .setRequestHeader "Content-Type", "multipart/form-data; boundary=" & STR_BOUNDARY 
    .send pvToByteArray(sPostData) 
End With 

Private Function pvToByteArray(sText As String) As Byte() 
    pvToByteArray = StrConv(sText, vbFromUnicode) 
End Function 

錯誤:

responseText 
{"message":"file.not_found","error":"There is no file in request.","status":400,"cause":[]} 

謝謝!

回答

3

如果你只需要在這裏發表一個文件是一個簡單的函數

Private Sub pvPostFile(sUrl As String, sFileName As String, Optional ByVal bAsync As Boolean) 
    Const STR_BOUNDARY As String = "3fbd04f5-b1ed-4060-99b9-fca7ff59c113" 
    Dim nFile   As Integer 
    Dim baBuffer()  As Byte 
    Dim sPostData  As String 

    '--- 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 
    With CreateObject("Microsoft.XMLHTTP") 
     .Open "POST", sUrl, bAsync 
     .SetRequestHeader "Content-Type", "multipart/form-data; boundary=" & STR_BOUNDARY 
     .Send pvToByteArray(sPostData) 
    End With 
End Sub 

Private Function pvToByteArray(sText As String) As Byte() 
    pvToByteArray = StrConv(sText, vbFromUnicode) 
End Function 

您可以切換到MSXML2.ServerXMLHTTP,以便能夠使用其SetTimeouts方法,如果你的文件是足夠大,並且在30秒超時。

+0

responseText的 { 「消息」: 「file.not_found」, 「錯誤」: 「有是在要求沒有文件。」, 「狀態」:400, 「病因」:[]} 它說那是什麼。 .. :( –

+1

我已使用新代碼編輯帖子,請使用..請檢查!謝謝! –

+1

在原始代碼中,您使用'res.AddFile(「file」,「File_location_on_PC」)'where'file'是表單元素的名稱,所以在'Content-Disposition:form-data; name =「」file「」...'中匹配它,除非有一個我們不知道的參數'image002'。 – wqw