How to do an image upload in web.py and save it to the disk
I need to create a form and have the form upload an image and save it to my disk. Here is my code
import web
urls = (
开发者_开发百科'/hello', 'Index',
'/file_upload_form', 'ThatFile'
)
app = web.application(urls, globals())
render = web.template.render('templates/', base = "layout")
class ThatFile(object):
def GET(self):
return render.file_upload_form()
def POST(self):
form = web.input(image = "loc")
open(form.image,'r')
image_o = form.image.read()
return render.thatfile(o_image = o_image)
class Index(object):
def GET(self):
return render.hello_form()
def POST(self):
form = web.input(name = "Nobody", greet = None)
greeting = "%s, %s" % (form.greet, form.name)
return render.index(greeting = greeting)
if __name__ == "__main__":
app.run()
I have tried using PIL Image module but it does not display the image.
Use something like
import shutil
# SKIPPED
def POST(self):
form = web.input(image={})
with open('path/to/image.jpg', 'wb') as saved:
shutil.copyfileobj(form['image'].file, saved)
精彩评论