这里问的问题是:如何使用 VBA 从 Excel 向服务器发送 HTTP POST 请求? https://stackoverflow.com/questions/158633/how-can-i-send-an-http-post-request-to-a-server-from-excel-using-vba几乎正是我正在寻找的东西,除了我试图将许多文件发送到服务器。我进一步搜索并发现如何使用 VBA 通过 HTTP post 上传 zip 文件? https://stackoverflow.com/questions/3939578/how-do-i-upload-a-zip-file-via-http-post-using-vba这也很好,但相当令人沮丧 - 看起来工作量很大(不仅仅是在这里制作 HTML 表单......)。
选项#2在这里:http://www.motobit.com/tips/detpg_post-binary-data-url/ http://www.motobit.com/tips/detpg_post-binary-data-url/(如上面提到的 SO 问题中所引用的)似乎它可以很好地工作,但是当我在 JS 和 CSS 中工作时,我不知道如何在示例中创建 FormData (发送到服务器的二进制文件)。
谁能帮帮我吗?本质上,我想通过 VBA 通过 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>
谢谢大家。
编辑——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
脚本底部的消息框输出服务器的标头和响应(空白 HTML 页面)。我觉得我没有在标头中设置一些东西以使服务器满意(注意:尝试注释掉 Content-Type)。
如果有人有使用 VBA/6 中的 WinHttpRequest 对象通过 HTTP POST 二进制文件的经验,请帮忙! :)