如何使用 VBA 通过 HTTP post 上传 zip 文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3939578/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 12:11:16  来源:igfitidea点击:

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

vbafile-uploadadodbhttp-postwinhttp

提问by GeneQ

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. ;-)

这个问题已经被其他人以某种形式问过很多次,但大多数都没有得到回答,或者给出的答案是“使用 C#,废话!”,顺便说一句,这是一个非答案。;-)

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.

我想通过 VBA 将 zip 文件上传到 Web 服务器。服务器端代码已经存在并且运行良好;它通过手动表单上传接受 zip 文件,并对 zip 文件的内容进行处理。

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.

理论上,我计划将 zip 文件的二进制内容转换为 HTTP 请求字符串,并使用 WinHTTP 库中的一些方法将其发送到服务器。如果一切顺利,服务器端脚本(在 Perl 中)应该无法判断文件是来自 VBA 还是浏览器并继续正常工作。

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

但是,将 zip 文件转换为 HTTP 请求字符串似乎不是很简单。

Searching online seems to indicate there exists 3 common strategies:

在线搜索似乎表明存在 3 种常见策略:

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

方法 1:WinHTTP 和手动将二进制编码为 HTTP 请求字符串

  • 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.
  • 涉及以二进制模式打开文件并手动应用一些编码伏都教将二进制流更改为 HTTP 请求字符串,并使用 WinHTTP 将其发送出去。可怕的代码量。

Method 2: WinHTTP

方法二:WinHTTP

  • Involves using the ADODB.Stream. Less than ten lines of code.
  • 涉及使用 ADODB.Stream。不到十行代码。

Method 3: Automate IE via SendKeys

方法 3:通过 SendKeys 自动化 IE

  • 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.
  • 只能用一个词来形容这个黑客:呸!并且可能会在未来版本的 IE 中崩溃,如果还没有的话。不到十行代码。安全风险。

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 2,但是,文档很薄,代码示例很少,并且不确定它是否有效。大多数代码示例都不完整,并且经常有注释说它们不起作用。这个方法真的有效吗?

Method 1is next in line.

方法 1是下一个。

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

方法 3请不要!(如果涉及到,最好使用 C#。喜欢 C#,只是要求只允许 VBA)

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

任何人都有关于如何完成这项任务的好例子?

回答by Chris Rae

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:

在对上述所有方法(以及更多方法)进行了大量实验之后,我使用的内容与您的“方法 1”几乎相同 - 我正在使用来自http://www.motobit.com/ 的第(2) 项tips/detpg_post-binary-data-url/,但具有以下分配:

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

...而不是该代码中建议的代码(我认为该代码有点老了)。但是,它并不完美 - 例如,到目前为止,我一直无法在我拥有的旧 Windows XP 机器上运行它(在 Win7 上运行正常)。