简体   繁体   English

使用web.py和Python以HTML格式进行文件上传字段验证

[英]File Upload field validation in HTML form using web.py and Python

I'm using Python 2.7 and web.py to build a website that allows user to upload a picture file, specify a file location / file name, then save the picture file to that location / name. 我正在使用Python 2.7和web.py构建一个网站,该网站允许用户上传图片文件,指定文件位置/文件名,然后将图片文件保存到该位置/名称。

I want to validate 2 things and through up an error message in case the validation does not check out: 1. validate a file has been selected on the form 2. validate the file location text box has been populated 我想验证2件事,并通过一条错误消息进行验证,以防验证未检出:1.验证表单上已选择了一个文件2.验证文件位置文本框是否已填充

I have no problem with 2. However, validation of the file upload control on the form does not work. 我对2没有任何问题。但是,对表单上的文件上传控件的验证不起作用。 It always thinks that no file has been selected, so returns the error, even when a file has been correctly selected. 它始终认为未选择任何文件,因此即使已正确选择了文件,也返回错误。

I'm attaching my code below for my app.py and the index.html that contains the form. 我在下面为我的app.py和包含表单的index.html附加我的代码。 Any pointers greatly appreciated! 任何指针,不胜感激!

app.py app.py

import web
import shutil

# The (.*) along with the name parameter in the def GET method line
# allows to enter parameter into web address without the
# ?parameter=xxx syntax.

#urls = (
#    "/(.*)", "Index")

urls = (
    "/", "Index")

app = web.application(urls, globals())


render = web.template.render("templates/", base="boilerplate")


class Index(object):

 #   def GET(self, name):
  #      return render.index(name)

    def GET(self):
        return render.index()


    def POST(self):

        form = web.input(myimage={}, fileloc=None)

# The following line validates whether the fileloc text box has been populated.
# I don't know how to also validate that the file upload field has been populated!
# The value for the control always shows as empty, even if a file has been selected!
# Currently, if the file control is empty, the program will run but save an empty file.        

        if form.fileloc and form.myimage:

            with open(form["fileloc"], "wb") as saved:
                shutil.copyfileobj(form["myimage"].file, saved)

        else:
            #return render.error()
            return "You must populate the file location and upload a file"

        return render.index()


if __name__ == "__main__":
    app.run()

index.html index.html

<form action="/" method="POST" enctype="multipart/form-data">
    Select the file to be uploaded: <input type="file" name="myimage"><br><br>
    Enter full path and filename to save the file:    <input type="text" name="fileloc"
        size=75 value="/users/cdignam/desktop/picture.jpg"
        STYLE="background-color: yellow"><br><br>
    <input type="submit" value="Upload">
</form>

Try replacing 尝试更换

f form.fileloc and form.myimage:

with

if form.fileloc and form["myimage"].filename:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM