Search code examples
pythonweb2py

Web2py: How to get items in FieldStorage?


I have a form that accepts image uploads:

<form name="upload" enctype="multipart/form-data" method="post" class="form-horizontal">
    <div class="control-group">
        <div class="span2">
            <label for="image" class="control-label">Upload image:</label>
        </div>
        <div class="span10">
            <input id="image" name="image" type="file" class="span7" accept="image/*"/>
        </div>
    </div>
     <div class="form-group">
        <div class="span2"></div>
        <div class="span10">
          <button class="btn btn-medium btn-primary" type="submit">Submit</button>
        </div>
      </div>   
</form>

When I request.vars['image'], the following is returned:

FieldStorage('image', 'a.png', '\x89PNG\r\n\x1a\n\x00...')

How do I access these items? If I attempt to work with it as I would a dict, I receive an error that the object is not indexable. I've never worked with FieldStorage before, so I'm not exactly sure what I need to do to access this data.


Solution

  • If anyone else is interested, this worked:

    request.vars['image'].filename
    request.vars['image'].value
    

    For the file name and binary data, respectively. Just needed a quick summary of the available attributes: http://python.about.com/od/cgiformswithpython/ss/pycgitut1_3.htm