【问题标题】:Flask validate and upload file to web page to烧瓶验证并将文件上传到网页以
【发布时间】:2018-06-04 19:40:48
【问题描述】:

我一直在尝试将文件上传并使用到我的网页,但不断遇到错误。我反复搜索并尝试了许多不同的解决方案,但均未成功。任何方向将不胜感激。

/app/forms.py

class UploadForm(FlaskForm):
    cadfile = FileField('cadfile', validators=[FileRequired(), FileAllowed(['STEP','STP'], 'Step Files Only')])
    upload = SubmitField('Upload')

    def __init__(self, *args, **kwargs):
        super(UploadForm, self).__init__(*args, **kwargs)
        self.user = None

    def validate(self):
        initial_validation = super(UploadForm, self).validate()
        if not initial_validation:
            print('Fails')
            return False
        return True

这部分代码每次在“not initial_validation”时都会失败,无论没有文件、文件类型不正确还是文件类型正确。

/app/routes.py

@app.route('/index', methods=['GET', 'POST'])
def index():
    form = UploadForm()
    if form.validate_on_submit():
        if 'cadfile' in request.files:
            cadfile = request.files['cadfile']
            cadfile.save('/app/tmp/' + cadfile.filename)
            print('Success')
        else:
            print('No Go')
        return redirect(url_for('details'))
    return render_template('index.html', title='Home', form=form)

还有我的 html 文件 /app/templates/index.html

<html>
    <head>
        <title>Upload</title>
    </head>
    <body>
        <h1>Let's Do This</h1>
        <form method="post" enctype="multipart/form-data">
            {{ form.hidden_tag() }}
            {{ form.cadfile }}
            {{ form.upload() }}
        </form>
    </body>
</html>

我想让它在没有选择文件时打印出“无文件”,当不是 STP 或 STEP 文件时打印出“无效文件类型”,当文件成功上传时打印出“成功”。

【问题讨论】:

    标签: html python-3.x flask upload wtforms


    【解决方案1】:

    搞定了。这是我的最终代码

    /app/forms.py

    class UploadForm(FlaskForm):
       print('called uploadform')
       cadfile = FileField('cadfile', validators=[FileRequired()])
       upload = SubmitField('Upload')
    

    /app/routes.py

    def index():
       form = UploadForm()
       FILE_TYPES = set(['STEP'])
       var.company_id = current_user.company_id
       if form.validate_on_submit():
           print('Validated')
           submit_name = form.cadfile.data.filename
           if '.' in submit_name and submit_name.rsplit('.', 1)[1] in FILE_TYPES:
               print('Yea Baby it is!')
               ...
               code
               ...
               return redirect(url_for('details'))
           else:
               form.cadfile.errors.append('File is not an accepted format')
       return render_template('index.html', title='Home', form=form)
    

    /app/templates/index.html

           <form method="post" enctype="multipart/form-data">
               {{ form.hidden_tag() }}
               <p>
                   {{ form.cadfile }}
                   {{ form.upload() }}<br>
                   {% for error in form.cadfile.errors %}
                   <span style="color: red;">[{{ error }}]</span>
                   {% endfor %}
               </p>
           </form>
       </body>
    

    谢谢

    【讨论】:

      猜你喜欢
      • 2020-08-30
      • 2021-07-03
      • 1970-01-01
      • 2017-12-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多