On 3/7/06, Tom Lesterswrote: > > Hi All, > > Seems upload binary files should be really easy in Quixote, > There are Upload class, > But I could not find how to receive, save file in the demo > Look at the source code, seem it's more about txt file. > the example on quixote cookbook is out dated: there is no tmp_filename > anymore: > > http://www.quixote..ca/qx/UploadingFiles > def count (request): > upload = request.get_field('file') > lines = 0 > for line in open(upload.tmp_filename, 'r'): > lines += 1 > return " The file is %i lines long." % lines > > Anybody can point to me 3-5 line code of checking the binary file size then > save it ? To get the file size from a file widget value ('upload' variable): 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) I've proposed having the Quixote set the size as 'upload.size' for this purpose, but it hasn't been implemented. Then to save the upload in a file: f = 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() -- Mike Orr
(mso@oz.net address is semi-reliable)