开发者

Upload file via HTTP from VBA (WinHTTP)

I'm trying to (HTTP) upload a binary file programmatically from within VBA. I intend to put an ASPX page on the server to accept the file and certain additional parameters.

I know there are lots of nice ways to do that (e.g. use web service instead of aspx), but my constraint is that it must run in VBA (in an excel file), and that I cannot install any additional components on the client.

So I guess I'll use WinHTTP, and I've found several examples to post form data, but not to post a binary file. I probably need to base64 the file contents?

So my questions are:

  • Do I need to do the encoding manually or can I make WinHTTP do that?
  • Is there a better utility to use than WinHTTP? (Remember I don't want to install any additional software, it must be shipped with WinXP Pro, Office 2007 or a .NET framework, e.g.)
  • Is there a better way to go, e.g. using ASP.NET web s开发者_如何学Pythonervices?

Thx, chiccodoro


You may use base64 but typically writing binary is easier.

The hurdle you have to leap is constructing a valid multi-par/form POST. This is completely possible using WinHTTP, although I have not done it in years and am not tooled to provide sample code, it is not trivial.

You can reference the following articles for examples of how to do this with C# HttpWebRequest. The WinHTTP api is a bit different of course but the salient points to take away from the articles is the structure of the POST body.

  • C# File Upload with form fields, cookies and headers (by yours truly)
  • UploadFileEx: C#'s WebClient.UploadFile with more functionality (a bit more procedural and may be easier to suss out the format)

Typically I provide sample code, but as I said, I do not have any stone-age tools set up right now ;-).

HTH

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜