datasci
datasci

Reputation: 1029

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.

Upvotes: 1

Views: 1195

Answers (2)

David_O
David_O

Reputation: 1153

This is extremely helpful if you are trying to check some aspects of a file before trying to process a form. I wanted to get the sha256 hash of an uploaded file and make sure it hadn't been uploaded before. The upload is in a field Field('file', 'upload').

Originally, I used the following, but this consumes the data in request.vars.file so that, after successful processing, the file written to disk is empty.

file_contents = request.vars.file.read()
form.vars.file_hash = hashlib.sha256(file_contents).hexdigest()
form.vars.file_length = len(file_contents)

However, in the updated code below datasci's answer allows you to access the data without consuming it from request.vars.file. Might be obvious, but it took a long time for me to figure out what was going on!

# Is there a file - the value will be None when the page first loads and
# can be 'str' if submit is pressed without a file selected. 
if request.vars.file != None and not isinstance(request.vars.file, str):
    form.vars.file_name = request.vars.file.filename
    form.vars.file_hash = hashlib.sha256(request.vars.file.value).hexdigest()
    form.vars.file_size= len(request.vars.file.value)
if form.process(onvalidation=validate_dataset_upload).accepted:
    # notify upload has worked
    response.flash = ('Upload successful. A validation check will be run and '
                      'you will get an email with the results when it finishes.')

All of this means that the validate_dataset_upload function can now check form.vars.file_hash does not already exist in the underlying table.

Upvotes: 1

datasci
datasci

Reputation: 1029

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

Upvotes: 8

Related Questions