简体   繁体   English

VBA POST请求发送包含上载文件的多部分表单数据

[英]VBA POST request sending multipart form data with uploading file

I'd love to get some input on this subject particularly for Excel/VBA. 我很乐意就此主题提供一些意见,尤其是对于Excel / VBA。

By using the network traffic monitor I've figured out that a system at work relies on POST requests sending multipart/form data. 通过使用网络流量监视器,我发现工作中的系统依赖于POST请求发送多部分/表单数据。 I've cracked all the syntax with the boundaries etc and have replicated string building for forms with loads of different fields perfectly with successful uploads and responses. 我用边界等方法破解了所有语法,并成功完成了上传和响应,为具有不同字段负载的表单复制了字符串构建。

The one thing I am struggling with is how to include uploaded files. 我苦苦挣扎的一件事是如何包括上载的文件。 When I see this on the request body in the traffic monitor it looks something like this: 当我在流量监控器的请求正文中看到此消息时,它看起来像这样:

-----------------------------7d01ecf406a6
Content-Disposition: form-data; name="Reload"

False
-----------------------------7d01ecf406a6
Content-Disposition: form-data; name="ReferenceFile001"; filename="word.doc"
Content-Type: msoffice/word

<Binary File Not Shown>
---------------------------7d01ecf406a6    'After a file entry boundary not prefixed with 2 "-"
Content-Disposition: form-data; name="SomethingElse"

SomeRandomStuff
-----------------------------7d01ecf406a6--

So I did some digging and found several examples of converting files to binary and including them in the string, before converting the entire thing using 'strConv(theString, vbFromUnicode) 因此,我做了一些挖掘工作,并找到了一些将文件转换为二进制文件并将其包含在字符串中的示例,然后再使用'strConv(theString,vbFromUnicode)转换整个内容。

But I haven't had any successful uploads of files using this method. 但是我没有使用这种方法成功上传任何文件。 I've had various server reply errors as I've tried different things but now I'm at a point where I don't get any error response... but also the upload just simply wasn't successful. 我尝试过不同的操作时遇到了各种各样的服务器回复错误,但是现在我无法得到任何错误响应...但是上传只是简单地没有成功。

The code I'm currently using is similar to the one found Here! 我当前使用的代码类似于在这里找到的代码 and I've included the binary conversion bit below. 并且我在下面包括了二进制转换位。 I've also tried this one using recordsets and appending chunks but kept getting errors here 我也尝试过使用记录集和附加块,但在这里不断出错

Basically I want to know if anyone out there has done this successfully in Excel/VBA - multipart forms with various fields, some of which are file uploads. 基本上,我想知道是否有人在Excel / VBA中成功完成了此任务-具有多个字段的多部分表单,其中一些是文件上载。 Is there an easy technique to convert a file to binary and include it in a text string? 是否有一种简单的技术将文件转换为二进制文件并将其包含在文本字符串中? because my main question regarding the code below is that we have a normal text string and stick a converted binary file in the middle of it, then convert that again before sending. 因为关于以下代码的主要问题是我们有一个普通的文本字符串,并将转换后的二进制文件粘贴在其中,然后在发送之前再次进行转换。 Does it even make sense to stick converted files like this in the middle of a string?? 像这样将转换后的文件粘贴在字符串中间甚至有意义吗? I ask this because when trying to view the string in a text file before I send it, I get an error on trying to write the "string" to a text file! 我之所以这样问,是因为在发送之前尝试查看文本文件中的字符串时,在尝试将“字符串”写入文本文件时出现错误!

I hope this makes sense and any contributions at all would be appreciated, I've been scratching my head over this for days now. 我希望这是有道理的,所有的贡献都将不胜感激,我已经为此努力了几天。

stringToSend = String1 & getFile(thisworkbook.path & "filename.doc" & String2  '' Assume string1 and string2 are perfect,
'' I have this working even with file "deletions" where the following "getFile" function would return ""

Function GetFile(ByVal FileName As String) As String
    Dim FileContents() As Byte, FileNumber As Integer
    ReDim FileContents(FileLen(FileName) - 1)
    FileNumber = FreeFile()
    Open FileName For Binary As FileNumber
    Get FileNumber, , FileContents
    Close(FileNumber)
    GetFile = StrConv(FileContents, vbUnicode)
End Function

forget GetFile function ! 忘记GetFile函数! Because data send to Server MUST be a Byte() array , not a String ! 因为发送到服务器的数据必须是Byte()数组,而不是String! See the last code for Jscript at this link , easily to convert it to VBA . 此链接中查看Jscript的最后代码,可以轻松地将其转换为VBA。 If you cannot success upload file , i will share you link excel file contain demo success code Upload file . 如果您无法成功上传文件,我将与您共享包含演示成功代码的excel文件的链接。

My own answer, since I managed to get this system to work: 我自己的回答,因为我设法使该系统正常工作:

After the concatenation of the various Strings and GetFile(s), the final step was to use below just before sending in a POST request 在将各种Strings和GetFile串联之后,最后一步是在发送POST请求之前在下面使用

strConv(sendRequest, vbFromUnicode)

I'm sure I had tried this a few times anyway, but perhaps my string building was missing a VbCrLf or something. 我确信无论如何我都尝试过几次,但是也许我的弦乐构建缺少VbCrLf之类的东西。 Anyway, it DOES work! 无论如何,它确实有效! Yay! 好极了!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 VBA发布请求错误-多部分/表单数据 - VBA post request error - multipart/form-data 通过带有 vba 的 POST 请求发送 csv 文件 - Sending a csv file via POST request with vba 将多部分/表单数据发布到端点 - Post multipart/form-data to endpoint Powershell:如何在多部分/表单数据请求中发布 excel (.xlsx) 文件? - Powershell: How can I POST excel (.xlsx) files in a multipart/form-data request? 如何使用python中的POST请求在multipart/form-data中发送excel文件? - How to send excel file in multipart/form-data with POST Requests in python? 从 ASP.NET Core HttpContext.Request 的 multipart/form-data 内容读取 excel 文件? - Reading excel file from ASP.NET Core HttpContext.Request of multipart/form-data content? 在 VBA 中通过 XMLHTTP 发送表单数据 - Sending Form Data Through XMLHTTP in VBA 使用vba将带有当前excel表的Post请求作为多部分表单数据发送 - Send a Post request with the current excel sheet as multi-part form data using vba 如何使用 WinHTTPRequest 在 Excel 中使用 VBA 发送表单数据 POST 请求 - How to send a form-data POST request with VBA in Excel using WinHTTPRequest 如何使用Power Query的Web发布目录/表单数据 - How to POST a multipart/form-data using Power Query's Web.Contents
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM