我在 Visual Basic .net 中有这段代码,我想在 Visual Basic 6 中做同样的事情。代码将图像上传到给定的 URL。我必须使用 Rest 协议,因为我上传图像的服务器请求它。
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":[]}
谢谢!