title: Post an InMemoryUploadedFile from a file input field to another external server
body: <p>I have an image file which is selected from an input field and posted via a form in django.</p>
  
  <p>The form data and the image I want to post to another server
  .</p>
  
  <p>The problem seems to be, the file data in the request is an InMemoryUploadedFile type, which need to be transformed together with other posted data (json format).</p>
  
  <p>basically my code is:</p>
  
  <pre><code>from poster.encode import multipart_encode
  from poster.streaminghttp import register_openers
  
  def some_view(request):
      files=None
      if request.FILES:
          key = 'expected_image_name' #something else here, but not relevant
          if key in request.FILES:
              memfile = request.FILES.get(key, None)
              if memfile:
                  files={}
                  # note this works: but does not upload the file:
                  # files["image"]= memfile.read()
                  # So trying a lot of other things, like:
                  files["image"]= ContentFile(memfile.read(), name=memfile.name).read()
      # I also retrieve some data from request.Post... but for simplycity lets say it is:
      post_values = {"some_other_field": "whatever"}
      register_openers()
      url="My post url"
      # for simplycity I leave out the authentication, the problem is not in there
      if files:
          multi_data=post_values
          multi_data.update(files)
          datagen, headers = multipart_encode(multi_data)
          request = urllib2.Request(url, datagen, headers)
          response = urllib2.urlopen(request)
      else:
          request = urllib2.Request(url)
          response = urllib2.urlopen(request,data)
  </code></pre>
  
  <p>Whatever I try... the file does not get uploaded to the server.</p>
  
  <p>logging the responese... the item is created... but the image = null</p>
  
  <p>anybody a suggestion of what I Am overlooking here....</p>
  
  <p><strong>EDIT Tuesday 2013 - 6 - 4</strong></p>
  
  <p>I placed this logging in the last: 'if files' part
      logger.debug('DEBUG: datagen: %s' % datagen)
      logger.debug('DEBUG: headers: %s' % headers)</p>
  
  <p>I got this over there:</p>
  
  <pre><code>DEBUG get_response: datagen: &lt;poster.encode.multipart_yielder instance at 0x7fdb7cca4200&gt;
  DEBUG get_response: headers: {'Content-Length': '30349', 'Content-Type': 'multipart/form-data; boundary=baa9e321554a49c6b626c68860a90aaf'}
  </code></pre>
  
  <p>And more info:
  I Am sending it from jquery input form</p>
  
  <p>the form in html looks like:</p>
  
  <pre><code>&lt;form data-ajax="false" id="form_5" method="post" enctype="multipart/form-data" action="My url to the view above"&gt;
      &lt;div style='display:none'&gt;&lt;input type='hidden' name='csrfmiddlewaretoken' value='z47GsVwsBOzu3HrUMIZaI4hoyV2ljLVa' /&gt;&lt;/div&gt;
      &lt;!-- and some other fields in input fields --&gt;
      &lt;input type="file" name="expected_image_name" data-role="button" id="expected_iamge_name" value="Select photo"/&gt;
      &lt;input type="submit" value="Submit"&gt;
  &lt;/form&gt;
  </code></pre>