title: Uploading Image MultiPart Form HTTPWebRequest - Getting Image Data To String?
body: <p>I am trying to upload a multi-part form with HTTPWebRequest &amp; all is ok until I added an image to upload, mainly I am trying to do exactly the same request as a browser makes which looks like this:</p>
  
  <pre><code>-----------------------------41184676334
  Content-Disposition: form-data; name="file"; filename="guitar tape.jpg"
  Content-Type: image/jpeg
  
  IMAGEDATAHERE
  -----------------------------41184676334
  Content-Disposition: form-data; name="save"
  
  save
  -----------------------------41184676334--
  </code></pre>
  
  <p>I am lost on how to format / read the image to set it into the request that I am making below:</p>
  
  <pre><code>            Dim boundary As String = "-----------------------------" &amp; DateTime.Now.Ticks.ToString("x")
          Dim req As HttpWebRequest = DirectCast(WebRequest.Create("http://www.mysite.com/upload.php"), HttpWebRequest)
          req.Method = "POST"
          req.ContentType = "multipart/form-data; boundary=" &amp; "---------------------------" &amp; DateTime.Now.Ticks.ToString("x")
          req.KeepAlive = False
          Dim builder As New StringBuilder()
          builder.Append(boundary &amp; vbCrLf &amp; "Content-Disposition: form-data; name=""variable1""" &amp; vbCrLf &amp; vbCrLf &amp; "1" &amp; vbCrLf)
          builder.Append(boundary &amp; vbCrLf &amp; "Content-Disposition: form-data; name=""file""; filename=""" &amp; FileName &amp; """" &amp; vbCrLf)
          builder.Append("Content-Type: application/octet-stream")
          builder.Append(vbCrLf &amp; vbCrLf)
          ' Add Photo Here
          If UpdateImage = True Then
              ' Load Image
              Dim ImageData As System.Drawing.Image
              Dim fs As New System.IO.FileStream(ImagePath, System.IO.FileMode.Open)
              ImageData = Image.FromStream(fs)
              fs.Close()
              ' Add Image To Header
              builder.Append(ImageData)
              builder.Append(vbCrLf)
          Else
              builder.Append(vbCrLf)
          End If
          builder.Append(boundary &amp; vbCrLf &amp; "Content-Disposition: form-data; name=""save""" &amp; vbCrLf &amp; vbCrLf &amp; "save")
          ' Footer Bytes
          Dim close As Byte() = Encoding.UTF8.GetBytes("--")
          Dim postHeader As String = builder.ToString()
          Dim postHeaderBytes As Byte() = Encoding.UTF8.GetBytes(postHeader)
          Dim boundaryBytes As Byte() = Encoding.ASCII.GetBytes(vbCrLf &amp; boundary &amp; "--" &amp; vbCrLf)
          Dim length As Long = postHeaderBytes.Length + boundaryBytes.Length
          req.ContentLength = length
          Dim requestStream As Stream = req.GetRequestStream()
          Dim fulllength As Integer = postHeaderBytes.Length + boundaryBytes.Length
          ' Write out our post header
          requestStream.Write(postHeaderBytes, 0, postHeaderBytes.Length)
          ' Write out the trailing boundary
          requestStream.Write(boundaryBytes, 0, boundaryBytes.Length)
          Dim responce As WebResponse
          responce = req.GetResponse()
          requestStream.Close()
          Dim s As Stream = responce.GetResponseStream()
          Dim sr As New StreamReader(s)
          Dim Content As String = sr.ReadToEnd()
  </code></pre>
  
  <p>At the moment it is simply posting "System.Drawing.Bitmap" as the image data but I am not sure how to get the same raw data for the image that looks like this:</p>
  
  <pre><code>    J©õÝ¨e‚Lnž¿Ëã/Ç§úÐ5ý¼C÷Cý&gt;ß’t;fm—=Äw:�/E±ËÙÏ$á@%Pc&gt;×    Šgw.²Ab“:ÅÓù:Ï¯Ìh6à€Z§Ó‚g£®hÚD6¨Ø^Ú2ô`ä¨L�YÆÄÅCX#I“ÈÌãj¦L˜•’|¥�Eb¡ëQ–¤Ú, 3\UzL  öÔoj4�•±’u«c¼#„oÕ`îF&gt;·o—ŠûÅ«ÎÑ™¶Ç˜ýº*i°œÈVŒ�Qû”Ñ[.�ÔmçE•ì¦eNCh�Ù
  é§�É$m¿ôš"»ÌNæ(VÌmp›F¹XÈ88™ªüµ…d•XµÔÜ#�ˆŠv‘º‚F‚§Yûb
  </code></pre>
  
  <p>Any ideas on how I could do this or would I need to change my methods?</p>