|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
http 请求服务器,其它的都正常,现在需要POST一个文件到服务器,本地文件,这个文件怎么处理,才可以装入POST,麻烦指教啊!网上找了很多代码,都不对啊,请大神赐教,谢谢。
错误代码如下:
Sub fileup()
SchemaCode = "D000867chen0527"
FilePropertyName = "photo"
BizObjectId = "b1b9ed47-151c-4447-92e9-c602cddc427b"
URL = "https://www.h3yun.com/OpenApi/UploadAttachment?SchemaCode=" & SchemaCode & "FilePropertyName=" & FilePropertyName & "BizObjectId=" & BizObjectId
cFile = "E:\image\322574.jpg"
Dim http As MSXML2.ServerXMLHTTP
Set http = New MSXML2.ServerXMLHTTP
http.Open "POST", URL, False
Dim b() As Byte, strFile As String
boundary = "----------" & RandomString()
postData = "--" & boundary & vbCrLf
postData = postData & "Content-Disposition: form-data; name=\""albumid\""" & vbCrLf
postData = postData & vbCrLf & "天aaaaa123" & vbCrLf
postData = postData & "--" & boundary & vbCrLf
postData = postData & "Content-Disposition: form-data; name=""photo[]""; filename=""" & cFile & """" & vbCrLf
postData = postData & "Content-Type: image/gif" & vbCrLf
ReDim b(FileLen(cFile))
ff = FreeFile
Open cFile For Binary As #ff
Get #ff, , b
Close #ff
strFile = b
postData = StrConv(postData & vbCrLf, vbFromUnicode) & LeftB(s, UBound(b)) & ChrB(13) & ChrB(10)
postData = postData & StrConv("--" & boundary & "--" & vbCrLf, vbFromUnicode)
http.setRequestHeader "Content-Type", "multipart/form-data; boundary=" & boundary
ReDim b(LenB(postData))
b = postData
HexStr = "" '这一段是我显示提交的数据
For i = 0 To UBound(b)
Debug.Print IIf(b(i) < 16, "0", "") & Hex(b(i)); " ";
HexStr = HexStr & IIf(b(i) < 32 Or b(i) > 127, ".", Chr(b(i)))
If i Mod 16 = 15 Then Debug.Print " " & HexStr: HexStr = ""
Next
http.setRequestHeader "Content-Length", UBound(b)
http.send b
http.waitForResponse
Debug.Print http.getAllResponseHeaders
Debug.Print StrConv(http.responseBody, vbUnicode)
End Sub |
|