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

这里提出的问题是: 如何使用VBA从Excel发送HTTP POST请求到服务器? 几乎正是我正在寻找,除了我试图发送一些文件到服务器。 我GOOGLE了进一步,发现如何使用VBA通过HTTP后上传一个zip文件? 这也不错,但相当令人沮丧 – 看起来像很多工作(不只是在这里做一个HTML表单…)。

选项#2在这里: http : //www.motobit.com/tips/detpg_post-binary-data-url/ (正如在SO上面提到的问题)似乎它会工作,但由于我工作在JS和CSS,我不知道如何创buildFormData(二进制文件发送到服务器)在这个例子中。

谁能帮帮我吗? 实质上,我希望通过HTTP_POST通过VBA从Excel内部发送3-6个文件到一个期望表单数据的PHP服务器上的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页面)。 我觉得有一些东西,我没有设置在标题中,使服务器开心(注意:尝试注释掉内容types)。

如果有人使用VBA / 6中的WinHttpRequest对象通过HTTP POST二进制文件,请帮忙! 🙂

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

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

我正在成功使用下面的一个(它使用ADODB.Stream和WinHttp.WinHttpRequest.5.1):

http://www.ericphelps.com/scripting/samples/reference/web/http_post.txt

(如果网站消失,也可在Internet Archive上find )