How do I upload a zip file via HTTP post using VBA?

百般思念 提交于 2020-02-22 06:33:05

问题


This question has been asked many times by others in some form or another but most remained unanswered or the given answer is "Use C#, duh!", which incidentally, is a non-answer. ;-)

I want to upload a zip file to a web server via VBA. The server side code already exists and works well; it accepts zip files via manual form upload and does something with the contents of the zip file.

The theory is, I plan to transform the binary contents of the zip file into a HTTP request string and send it to the server using some methods from the WinHTTP library. If everything goes well the server side script (in Perl) shouldn't be able to tell whether the file came from VBA or a browser and continue working normally.

However, transmogrifying the zip file to a HTTP request string doesn't seem to be very straight forward.

Searching online seems to indicate there exists 3 common strategies:

Method 1: WinHTTP and manual encoding of binary to HTTP request string

  • Involves opening the file in binary mode and manually applying some encoding voodoo to change the binary stream into a HTTP request string and send it on its way using WinHTTP. Scary amount of code.

Method 2: WinHTTP

  • Involves using the ADODB.Stream. Less than ten lines of code.

Method 3: Automate IE via SendKeys

  • There's only one word to describe this hack : Yuck! And probably will break in future version of IE, if not already. Less than ten lines of code. Security risk.

I leaning towards Method 2, however, documentation is thin, code examples rare, and there's no certainty that it works. Most code examples are incomplete and often have comments that say they don't work. Does this method actually work?

Method 1 is next in line.

Method 3 Please, no! (Rather use C# if it comes to that. Love C#, just the requirements only permit VBA)

Anyone has any good examples on how to accomplish this task?


回答1:


After a horrific volume of experimentation with all your methods above (and more), I'm using what pretty much amounts to your "Method 1" - I'm using item (2) from http://www.motobit.com/tips/detpg_post-binary-data-url/, but with the following assignment:

 Set http = CreateObject("WinHttp.WinHttprequest.5.1")

... instead of the one suggested in that code (the code is a little elderly, I think). It is not perfect, though - for example, I've been so far unable to get it working on an old Windows XP machine I had (works ok on Win7).



来源:https://stackoverflow.com/questions/3939578/how-do-i-upload-a-zip-file-via-http-post-using-vba

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!