Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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! :)

like image 728
jonathanbell Avatar asked Jun 08 '12 18:06

jonathanbell


2 Answers

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/

like image 53
Tim Williams Avatar answered Nov 09 '22 14:11

Tim Williams


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

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

(if website disappears, also available on Internet Archive)

like image 28
8128 Avatar answered Nov 09 '22 14:11

8128