0

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

2 Answers 2

1

forget GetFile function ! Because data send to Server MUST be a Byte() array , not a String ! See the last code for Jscript at this link , easily to convert it to VBA . If you cannot success upload file , i will share you link excel file contain demo success code Upload file .

Sign up to request clarification or add additional context in comments.

3 Comments

Hi, yesterday I managed to get this working successfully but after the GetFile and string concatenation, I had to then convert the whole lot using 'strConv' with 'vbFromUnicode' just before I sent it in a POST request. What a relief! Edit: And I just checked the link you posted and see that it essentially does the same thing, so I've accepted your answer.
jamheadart : strConv function NOT supports UTF-8 encoding !
Consider editing your post to add the information present in the comments to the post. Flag the comments as obsolete after doing so.
1

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

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. Anyway, it DOES work! Yay!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.