title: VBA POST request sending multipart form data with uploading file
body: <p>I'd love to get some input on this subject particularly for Excel/VBA.</p>
  
  <p>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.</p>
  
  <p>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:</p>
  
  <pre><code>-----------------------------7d01ecf406a6
  Content-Disposition: form-data; name="Reload"
  
  False
  -----------------------------7d01ecf406a6
  Content-Disposition: form-data; name="ReferenceFile001"; filename="word.doc"
  Content-Type: msoffice/word
  
  &lt;Binary File Not Shown&gt;
  ---------------------------7d01ecf406a6    'After a file entry boundary not prefixed with 2 "-"
  Content-Disposition: form-data; name="SomethingElse"
  
  SomeRandomStuff
  -----------------------------7d01ecf406a6--
  </code></pre>
  
  <p>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)</p>
  
  <p>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.</p>
  
  <p>The code I'm currently using is similar to the one found
  <a href="http://technet.rapaport.com/info/lotupload/samplecode/webclient_file_vba.aspx" rel="nofollow noreferrer">Here!</a> and I've included the binary conversion bit below.
  I've also tried this one using recordsets and appending chunks but kept getting errors <a href="http://www.motobit.com/tips/detpg_uploadvbsie/" rel="nofollow noreferrer">here</a></p>
  
  <p>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!</p>
  
  <p>I hope this makes sense and any contributions at all would be appreciated, I've been scratching my head over this for days now.</p>
  
  <pre><code>stringToSend = String1 &amp; getFile(thisworkbook.path &amp; "filename.doc" &amp; 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
  </code></pre>