How to send files via HTTP_POST with Excel using VBA?
Asked Answered
S

2

10

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

Scientific answered 8/6, 2012 at 18:35 Comment(7)
What kind of files: text or binary?Passive
Would you be able to send the files one by one? Or is that not an option?Navigation
@TimWilliams binary, excel files.Scientific
@Navigation Yes! I think that I could do that. But still, as I know only JS I don't know how to do that, or if it can be done. I get that I can send params but binary data????Scientific
This is VB6 but should also work in VBA/Excel: wqweto.wordpress.com/2011/07/12/…Passive
@TimWilliams I'll give it a whirl on Monday! Thank you!Scientific
I've finally got this working! It was a pain for sure! Used Wireshark to see the server's headers and then matched those in code. Finally working. @TimWilliams, your link was very helpful - thank you. Put it as an answer if you like, and I will mark it/up vote it. Cheers!Scientific
P
16

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/

Passive answered 13/8, 2012 at 23:13 Comment(2)
If you are using a 64 bit version of Excel then you will need to add PtrSafe to your declared functions. Example: Private Declare PtrSafe Function InternetAutodialMancilla
I can't get this code to work... What library references do I need to make?Mancilla
S
3

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)

Saccharoid answered 11/1, 2017 at 11:26 Comment(1)
But with no explanation how it is deployed? An example would have made this so much more useful.Palermo

© 2022 - 2024 — McMap. All rights reserved.