如何在web.py中上传图像并将其保存到磁盘

时间:2011-07-25 04:51:07

标签: python upload web.py

我需要创建一个表单并让表单上传图像并将其保存到我的磁盘。这是我的代码

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

我尝试使用PIL Image模块,但它不显示图像。

1 个答案:

答案 0 :(得分:6)

使用类似

的内容
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)