Okay, got it sorted (finally)!

Here's the code to do multiple fields with some random file uploads.
I hope this helps someone else.  (How does one go about getting this
added to the cookbook?)

I was wrong about the ** not being needed.  Yes, as a matter of fact,
I /am/ a goober.  Often.

A couple gotcha's not mentioned on the cookbook page:
1) inputs to web.input() *must* be str strings and not unicode
strings.  These are not the same things, apparently.  That wasn't
clear to me before...;^)
2) If your file fields are going to be getting binary data (uploading
images), you must put _unicode=True as the first argument to web.input
().

--------------------------------------------
import web

urls = ('/upload', 'Upload')

class Upload:
    def GET(self):
        # put the list of image names in the session
        # NOTE: These /must/ be str strings, not unicode!!!!
        # These are the names of the fields on the form.  They must be
stored in the session
        # so that they can be picked up during the post.  If the user
waits to submit these until
        # after the session dies, the file uploads won't work.
        web.config._session['images']=['image-2.0','image-4.0']

        return """<html><head></head><body>
<form method="POST" enctype="multipart/form-data" action="">
<p><input type="input" name="question-1.0" />First question
<p><input type="file" name="image-2.0" />First image
<p><input type="input" name="question-3.0" />Second question
<p><input type="file" name="image-4.0" />Second image
<br/>
<input type="submit" />
</form>
</body></html>"""

    def POST(self):
        # get the image names
        images = web.config._session['images']
        myfiles = {}
        for k in images:
            myfiles[k] = {}

        x = web.input(_unicode=True,**myfiles)
        web.debug(x['image-2.0'].filename) # This is the filename
        web.debug(x['image-2.0'].value) # This is the file contents
        web.debug(x['image-2.0'].file.read()) # Or use a file(-like)
object
        raise web.seeother('/upload')

if __name__ == "__main__":
   app = web.application(urls, globals())
   web.config._session = web.session.Session(app, web.session.DiskStore
('/tmp/sessions'), {'count': 0,'logged_in':0})
   app.run()
--------------------------------------------
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"web.py" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [email protected]
For more options, visit this group at http://groups.google.com/group/webpy?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to