使用excel vba将图像上传到Web

时间:2016-03-31 13:27:41

标签: excel vba excel-vba httprequest xwiki

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

curl -u Admin:admin -T C:\temp\wikiTable.jpg http://192.168.0.35:8080/xwiki/rest/wikis/xwiki/spaces/Main/pages/WebHome/attachments/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://192.168.0.35:8080/xwiki/rest/wikis/xwiki/spaces/Main/pages/WebHome/attachments/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

我在这里做错了什么想法?

2 个答案:

答案 0 :(得分:1)

以下工作最终:

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

答案 1 :(得分:0)

我建议您尝试调用方法private void FillWinemakerComboBox() { SqlCommand cmand = new SqlCommand("SELECT (Lname+', '+Fname+' '+Mname) AS combinedName, Lname, Fname, Mname FROM Winemaker", con); con.Open(); SqlDataReader sqlReader = cmand.ExecuteReader(); while (sqlReader.Read()) { //This is where the comboBox fills the names winemaker_comboBox.Items.Add(sqlReader["combinedName"].ToString()); //The part where I don't really know how to get the other values if (winemaker_comboBox.SelectedItem != DBnull.Value) { string lastname = sqlReader["Lname"].ToString(); string firstname = sqlReader["Fname"].ToString(); string middlename = sqlReader["Mname"].ToString(); } } sqlReader.Close(); con.Close(); } 而不是"POST",因为它可能区分大小写。