问题描述
这里提出的问题:几乎正是我正在寻找的,除了我正在尝试将多个文件发送到服务器。我进一步搜索了这也很好,但非常不鼓励 - 似乎很多工作(不只是在这里制作一个HTML表单)。
The question asked here: How can I send an HTTP POST request to a server from Excel using VBA? is almost exactly what I was looking for except that I am trying to send a number of files to the server. I googled further and found How do I upload a zip file via HTTP post using VBA? This was also good, but pretty discouraging - it seems like a lot of work (not just making an HTML form here...).
选项#2在这里:(如上所述的关于SO的问题中引用的)似乎会很好,但是当我在JS和CSS,我不知道如何在示例中创建FormData(要发送到服务器的二进制文件)。
Option #2 here: http://www.motobit.com/tips/detpg_post-binary-data-url/ (as cited in the question on SO noted above) seems like it would work well, but as I work in JS and CSS, I have no idea how to create FormData (the binary files to send to the server) in the example.
有人可以帮我吗?实质上,我想通过HTTP_POST通过VBA从内部的Excel发送3-6个文件到正在等待表单数据的Web服务器上的PHP脚本。一个HTML表单来处理它,如下所示:
Can anyone please help me? In essence, I want to send 3-6 files over HTTP_POST via VBA from inside Excel to a PHP script on a web server that is expecting form data such as . An HTML form to handle this would look like:
<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日
EDIT -- Aug. 2nd 2012
我仍然在努力解决这个问题。我不知道VBA / 6,几乎只是基本的JS,所以我有点迷失了。这是我迄今为止所做的:
I'm still trying to work on this issue. I don't know VBA/6, pretty much just basic JS so I am a little lost. Here is what I have done so far:
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)。
The message boxes at the bottom of the script do output the server's headers and response (blank HTML page). I feel that there is something that I am not setting in the headers to make the server happy (note: trying commenting out Content-Type).
如果有人在VBA / 6中使用WinHttpRequest对象通过HTTP来POST二进制文件,请帮忙! :)
If anyone out there has experience using the WinHttpRequest object in VBA/6 to POST a binary file via HTTP, please help! :)
推荐答案
这个(使用WinInet)是VB6,但也应该在VBA / Excel中工作:
This (using WinInet) is VB6 but should also work in VBA/Excel:
这篇关于如何使用VBA通过HTTP_POST与Excel发送文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!