I'd love to get some input on this subject particularly for 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. 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)
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. 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