upload multiple files in web.py
I like web.py, mainly because it's simple and quite pythonic. I wanted a fancy multiple file upload page - however docs on uploading multiple files were a bit flaky. Here is what I came up with.
import web
import os
URLS = ('/upload', 'Upload')
APP = web.application(URLS, globals())
class Upload:
def GET(self):
return """
<html>
<form method="POST" enctype="multipart/form-data" action="">
Select files: <input type="file" name="upload" multiple>
<input type="submit">
</form>
</html>
"""
def POST(self):
files = web.webapi.rawinput().get('upload')
if not isinstance(files, list):
files = [files]
for f in files:
# in my case, i want files to go to static/media
target = os.path.join('static', 'media', f.filename)
content = f.file.read()
with open(target, 'w') as f:
f.write(content)
return
if __name__=='__main__':
APP.run()