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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10954293/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 13:18:06  来源:igfitidea点击:

How to send files via HTTP_POST with Excel using VBA?

excelvbaexcel-vbahttp-postwinhttprequest

提问by jonathanbell

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...).

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

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.

此处的选项 #2:http: //www.motobit.com/tips/detpg_post-binary-data-url/(如上面提到的 SO 问题中所引用)似乎效果很好,但因为我在 JS 和CSS,我不知道如何在示例中创建 FormData(发送到服务器的二进制文件)。

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:

谁能帮帮我吗?本质上,我想通过 HTTP_POST 从 Excel 内部通过 VBA 将 3-6 个文件发送到 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>

Thank you all in advance.

谢谢大家。

EDIT -- Aug. 2nd 2012

编辑 - 2012 年 8 月 2 日

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:

我仍在努力解决这个问题。我不知道 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

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).

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

If anyone out there has experience using the WinHttpRequest object in VBA/6 to POST a binary file via HTTP, please help! :)

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

回答by Tim Williams

This (using WinInet) is VB6 but should also work in VBA/Excel:

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

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

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

回答by 8128

I'm successfully using the one below (it uses ADODB.Stream and WinHttp.WinHttpRequest.5.1):

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

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

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

(if website disappears, also available on Internet Archive)

(如果网站消失,也可以在 Internet Archive 上找到