How to send files via HTTP_POST using Excel using VBA?

The question asked here: How can I send an HTTP POST request to a server from Excel using VBA? - this is almost what I was looking for, except that I was trying to send several files to the server. I googled further and found How to upload a zip file via an HTTP message using VBA? This was also good, but rather discouraging - it seems like a lot of work (not just creating an HTML form here ...).

Option # 2 here: http://www.motobit.com/tips/detpg_post-binary-data-url/ (as mentioned in the SO question noted above), it looks like it will work fine, but since I work in JS and CSS, I do not know how to create FormData (binaries to send to the server) in the example.

Can anybody help me? Essentially, I want to send 3-6 files via HTTP_POST via VBA from within Excel to a PHP script on a web server that expects form data such as. The HTML form for processing will look like this:

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

Thanks to everyone in advance.

EDIT - August 2, 2012

I am still trying to work on this issue. I don't know VBA / 6, pretty much just basic JS, so I got 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

script ( HTML). , -, , ( : Content-Type).

- WinHttpRequest VBA/6 POST HTTP, , !:)

+5
2

( ADODB.Stream WinHttp.WinHttpRequest.5.1):

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

( - , -)

0

All Articles