开发者

How to do an image upload in web.py and save it to the disk

开发者 https://www.devze.com 2023-03-22 11:16 出处:网络
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

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)
0

精彩评论

暂无评论...
验证码 换一张
取 消