Download file over HTTP from VBA (WinHTTP)
I am trying to (HTTP) download a binary programmatically from VBA. I intend to host an ASPX page on a server to accept a file and some additional options.
I know there are many good ways to do this (like using a webservice instead of aspx), but my limitation is that it has to run in VBA (in an excel file) and that I cannot install any additional components per client.
So, I think I'll be using WinHTTP and I've found some examples for posting form data, but not posting a binary. I probably need to base64 the content of the file?
So my questions are:
- Do I need to do the encoding manually or can I get WinHTTP to do it?
- Is there a better utility than WinHTTP? (Remember, I don't want to install any additional software, it must be shipped using WinXP Pro, Office 2007, or the .NET framework, for example.)
- Is there a better way to go for example. using ASP.NET Web Services?
thanks chiccodoro
a source to share
You can use base64, but it's usually easier to write binaries.
The hurdle you have to jump around is creating a valid multipart POST message. This is perfectly possible with WinHTTP, although I haven't done it for years and I haven't been able to provide a sample code, it's not trivial.
You can refer to the following articles for examples of how to do this using C # HttpWebRequest. The WinHTTP api is slightly different, of course, but the main point to take away from the articles is the structure of the POST body.
- Loading C # file with field fields, cookies and headers (for real)
- UploadFileEx: C # WebClient.UploadFile with more functionality (slightly more procedural and might be easier to debug format)
I usually provide sample code, but as I said, I don't have the Stone Age tools installed right now; -).
NTN
a source to share