Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
92 views
in Technique[技术] by (71.8m points)

How to send files via HTTP_POST with Excel using VBA?

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

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.

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>

Thank you all in advance.

EDIT -- Aug. 2nd 2012

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

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

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...