[Welcome] [TitleIndex] [WordIndex


Users must be able to upload files to your application, where the files will then be processed in some way.


Quixote's Publisher class will automatically handle uploaded files for you. The form value for a file upload field is not a string containing the file's data; instead it's an instance of the Upload class, which is basically just a holder for a number of filenames.

For example, to count the lines in an uploaded file:

def count (request):
    upload = request.get_field('file')
    lines = 0
    for line in open(upload.tmp_filename, 'r'):
        lines += 1
    return "<p>The file is %i lines long." % lines

In Quixote 2.4, there is no tmp_filename, to read a binary file(Originally from Mike Orr kindly answering to my question, thanks Mike ):

   from quixote import get_field

   upload = get_field('file')
   pos = upload.fp.tell()  # Save current position in file.
   upload.fp.seek(0, 2)    # Go to end of file.
   size = upload.fp.tell() # Get new position (which is the file size).
   upload.fp.seek(pos, 0)  # Return to previous position.
   upload.size = size
   if size > max_size:
       msg = "The uploaded file is too big (size=%s, max=%s bytes)"
       msg %= (size, max_size)
       raise QueryError(msg)

   out = open(DEST_FILENAME, 'wb')
   # Copy file in chunks to avoid using lots of memory.
   while 1:
       chunk = upload.read(1024 * 1024)
       if not chunk:


I'm not sure if this example is for Quixote 2.0, but in 1.0, there is no request.get_field(...) method. Use request.get_form_var('file') instead.



2010-09-22 22:14