Sorry, I misread the post. You don't need generator if you store the whole
file in database.
And to process mime i used mimetypes.guess_type.
This code i use for upload (assuming we can have many file inputs with the
same name "files")
def process_upload(obj_type, obj_id):
i = web.webapi.rawinput()
try:
files = i.files
now = datetime.datetime.now()
path = os.path.join(str(now.year), str(now.month))
if not os.path.exists(os.path.join(config.upload_dir, path)):
os.makedirs(os.path.join(config.upload_dir, path))
if not isinstance(files, list):
files = [files]
for f in files:
if f.filename:
filetype, encoding = mimetypes.guess_type(f.filename)
if not filetype:
flash.set(u"<p>Incorrect file format.</p>")
continue
file_id = db.insert("files",
title=web.safeunicode(f.filename), obj_type=obj_type, obj_id=obj_id,
filetype=filetype, path=path, filename="")
filename = ("document_%d" % file_id) +
mimetypes.guess_extension(filetype)
f2 = open(os.path.join(config.upload_dir, path, filename),
'w')
while 1:
buf = f.file.read(1024 * 8)
if not buf:
break
f2.write(buf)
size = f2.tell()
f.file.close()
f2.close()
db.update("files", where="id=$file_id",
vars=dict(file_id=file_id), filename=filename, size=size)
except KeyError:
pass
--
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.