如何使用VBA通过HTTP_POST与Excel发送文件?

jon*_*ell 8 excel vba http-post excel-vba winhttprequest

这里提出的问题是:如何使用VBA从Excel向服务器发送HTTP POST请求?几乎正是我正在寻找的,除了我试图将大量文​​件发送到服务器.我进一步搜索,发现如何使用VBA通过HTTP帖子上传zip文件?这也很好,但非常令人沮丧 - 这似乎是很多工作(不仅仅是在这里制作HTML表单......).

这里的选项#2:http://www.motobit.com/tips/detpg_post-binary-data-url/(正如上面提到的关于SO的问题中所引用的)似乎它会运作良好,但是当我在JS工作时CSS,我不知道如何在示例中创建FormData(发送到服务器的二进制文件).

谁能帮帮我吗?本质上,我想通过VBA从HTTP内部通过HTTP_POST将3-6个文件从Excel内部发送到期望表单数据的Web服务器上的PHP脚本.处理此问题的HTML表单如下所示:

<form action="upload_file.php" method="post" enctype="multipart/form-data">
  <input name="userfile[]" type="file" /><br />
  <input name="userfile[]" type="file" /><br />
  <input name="userfile[]" type="file" /><br />
  <input type="submit" />
</form>
Run Code Online (Sandbox Code Playgroud)

谢谢大家.

编辑 - 2012年8月2日

我还在努力解决这个问题.我不知道VBA/6,几乎只是基本的JS所以我有点失落.这是我到目前为止所做的:

Sub HTTPInternetPutFile()

    ' create new object with WinHttpRequest for this operation
    Dim WinHttpReq As Object
    Set WinHttpReq = CreateObject("WinHttp.WinHttpRequest.5.1")
    Dim FormFields As String

    ' initialize variables that we will set and pass as parameters
    Dim sfpath
    Dim strURL As String
    Dim StrFileName As String


       StrFileName = "CLIPrDL.csv"
       sfpath = "C:\CLIPr\"
       strURL = "http://s0106001c10187ab1.gv.shawcable.net/uploadtest/upload_file.php"


       WinHttpReq.Open "POST", strURL, False


       ' Set headers
       WinHttpReq.setRequestHeader "User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)"
       WinHttpReq.setRequestHeader "Accept-Charset", "ISO-8859-1,utf-8"
       WinHttpReq.setRequestHeader "Content-Type", "multipart/form-data"
       ' WinHttpReq.setRequestHeader "Content-Type", "text/html;charset=UTF8"
       WinHttpReq.setRequestHeader "Content-Disposition", "form-data; name=""userfile[]"""

       ' I dont understand this... why use fileup??
       FormFields = """filename=" & StrFileName & """"
       FormFields = FormFields & "&"
       FormFields = FormFields & sfpath

       ' so comment it out for now
       ' WinHttpReq.Send FormFields
       WinHttpReq.Send sfpath & StrFileName

       ' output this var to a message box becuase I dont know what it does really
       MsgBox FormFields
       ' Display the status code and response headers.
       MsgBox WinHttpReq.GetAllResponseHeaders
       MsgBox WinHttpReq.ResponseText


End Sub
Run Code Online (Sandbox Code Playgroud)

脚本底部的消息框会输出服务器的标题和响应(空白HTML页面).我觉得有些东西我没有设置在标题中以使服务器满意(注意:尝试注释掉Content-Type).

如果有人在VBA/6中使用WinHttpRequest对象通过HTTP发布二进制文件的经验,请帮忙!:)

Tim*_*ams 14

这个(使用WinInet)是VB6,但也应该在VBA/Excel中工作:

http://wqweto.wordpress.com/2011/07/12/vb6-using-wininet-to-post-binary-file/