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.
orig_filename is the original filename as supplied by the HTTP client. This will vary depending on the client's operating system, so you might receive "C:\Data\contents.zip" or "/home/amk/contents.zip". Do not trust this filename in creating a file on your server, because a malicious client could set the filename to something harmful.
base_filename is a sanitized version of the original filename, taking only the last component of orig_filename and removing any unsafe characters. Characters in the resulting filename are limited to letters, digits, and a few punctuation marks; see the SAFE_CHARS variable in quixote.upload for the exact set of legal characters.
tmp_filename is the path to a temporary file on your system that contains the contents of the file.
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: break out.write(chunk) out.close() upload.close()
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.