我在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":[]}
謝謝!
responseText的 { 「消息」: 「file.not_found」, 「錯誤」: 「有是在要求沒有文件。」, 「狀態」:400, 「病因」:[]} 它說那是什麼。 .. :( –
我已使用新代碼編輯帖子,請使用..請檢查!謝謝! –
在原始代碼中,您使用'res.AddFile(「file」,「File_location_on_PC」)'where'file'是表單元素的名稱,所以在'Content-Disposition:form-data; name =「」file「」...'中匹配它,除非有一個我們不知道的參數'image002'。 – wqw