【问题标题】:Upload Excel xlsm file to php script using VBA使用 VBA 将 Excel xlsm 文件上传到 php 脚本
【发布时间】:2013-03-02 12:25:54
【问题描述】:

我想从 VBA 将 Excel xlsm 文件上传到 php 脚本。我找到了以下代码:

Dim WinHttpReq As Object
Set WinHttpReq = CreateObject("WinHttp.WinHttpRequest.5.1")

Dim strURL As String
Dim StrFileName As String
Dim FormFields As String
Dim path As String
Dim name As String

   StrFileName = "c:\temp\ctc1output.xls"
   strURL = "http://www.tri-simulation.com/P3/"


   WinHttpReq.Open "POST", strURL, False


   ' Set the header
  WinHttpReq.SetRequestHeader "Content-Type", "application/x-www-form-urlencoded"


   FormFields = """fileulp=" & StrFileName & """"
   FormFields = FormFields + "&"
   FormFields = FormFields + """sfpath=P3"""

   WinHttpReq.Send FormFields


   ' Display the status code and response headers.
   MsgBox WinHttpReq.GetAllResponseHeaders
   MsgBox WinHttpReq.ResponseText
  1. 我应该将文件作为二进制文件还是其他类型的文件来处理?
  2. 我能否在文件仍处于打开状态时上传文件(我想上传运行 VBA 的文件)?

我不确定我是否走在正确的轨道上。 我也不确定标题和表单字段应该是什么。

感谢任何帮助。

【问题讨论】:

  • 您好,从您的代码中,您只是将文件名发送到服务器。您需要以某种方式将文件转换为二进制文件并将它们包含在您的 POST 消息中。
  • 您好,我可以看到该文件必须作为二进制文件上传。我正在使用来自here 的代码。看来我也不需要复制。我现在的问题是我需要在帖子中发送一些文本。这将是一个单独的请求,还是我可以以某种方式将它包含在第一个请求中?对不起,如果我看起来多余,但这是我第一次接触这个话题。
  • 你必须模拟一个浏览器组装一个 POST 请求并对你的文件进行 base64 编码,这可以完成但有点乏味。另一种选择是运行curl,它只需要文件名和上传表单参数(curl 可执行文件可以从 vba 运行)。

标签: vba excel winhttp winhttprequest


【解决方案1】:

您不需要对任何内容进行 base64 编码。按照sample code you have found 但在准备文件之前(在 '---prepare body 评论之前)只需像这样添加您的其他文本(表单条目)

sPostData = sPostData & "--" & STR_BOUNDARY & vbCrLf & _
     "Content-Disposition: form-data; name=""" & Name & """"
sPostData = sPostData & vbCrLf & vbCrLf & _
     pvToUtf8(Value) & vbCrLf

... 其中NameValue 是设计名称和您要包含在服务请求中的实际文本。对于函数pvToUtf8 的实现,请查看this Google Cloud Print service connector。上面的sn-p取自同一连接器的pvRestAddParam函数。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-09-29
    • 1970-01-01
    • 2019-03-04
    • 1970-01-01
    • 2014-11-26
    • 2019-12-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多