【问题标题】:How to send files via HTTP_POST with Excel using VBA?如何使用 VBA 通过 HTTP_POST 和 Excel 发送文件?
【发布时间】:2012-06-12 20:01:42
【问题描述】:

这里提出的问题:How can I send an HTTP POST request to a server from Excel using VBA? 几乎正是我所寻找的,只是我试图将一些文件发送到服务器。我进一步搜索并发现How do I upload a zip file via HTTP post using VBA? 这也很好,但非常令人沮丧 - 似乎需要做很多工作(不仅仅是在这里制作 HTML 表单......)。

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

谁能帮帮我?本质上,我想通过 VBA 通过 HTTP_POST 从 Excel 内部将 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>

提前谢谢大家。

编辑 -- 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 二进制文件的经验,请帮助! :)

【问题讨论】:

  • 什么样的文件:文本或二进制文件?
  • 你能把文件一个个发过来吗?或者这不是一个选择?
  • @TimWilliams 二进制,excel 文件。
  • @ikh 是的!我认为我可以做到这一点。但是,由于我只知道 JS,我不知道该怎么做,或者是否可以做到。我知道我可以发送参数但二进制数据???
  • 这是 VB6,但也可以在 VBA/Excel 中工作:wqweto.wordpress.com/2011/07/12/…

标签: vba excel http-post winhttprequest


【解决方案1】:

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

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

【讨论】:

  • 如果您使用的是 64 位版本的 Excel,那么您需要将 PtrSafe 添加到您声明的函数中。示例:私有声明 PtrSafe 函数 InternetAutodial
  • 我无法让这段代码工作...我需要制作哪些库引用?
【解决方案2】:

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

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

(如果网站消失,也available on Internet Archive

【讨论】:

  • 但是没有解释它是如何部署的?一个例子会让这变得更加有用。
猜你喜欢
  • 2017-09-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-03
  • 1970-01-01
  • 1970-01-01
  • 2020-10-08
  • 1970-01-01
相关资源
最近更新 更多