如何使用flask-wtf上传多个文件? [英] How to upload multiple files with flask-wtf?

查看:97
本文介绍了如何使用flask-wtf上传多个文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用flask-wtf上传多个文件.我可以毫无问题地上传单个文件,并且已经能够将html标记更改为接受多个文件,但是到目前为止,我获得的文件还远远超过第一个文件.

I am trying to upload multiple files with flask-wtf. I can upload a single file with no problems and I've been able to change the html tag to accept multiple files but as of yet I haven't been able to get more than the first file.

附带的代码将给我第一个文件,但我不知道如何从中获取更多文件.我怀疑"render_kw = {'multiple':True}"只是更改了HTML标签,因此我可能会用这种方法来弄错树.我也偶然发现了wtforms的"MultipleFileField",但似乎无法返回任何文件,再次可能是因为它与我要使用的flask_wtf配合不好.有什么好方法吗?

The attached code will give me the first file but I can't figure out how to get any more files from it. I suspect that "render_kw={'multiple': True}" just changes the HTML tag so I might be barking up the wrong tree with this approach. I have also stumbled across "MultipleFileField" from wtforms but I can't seem to get that to return any files, again likely since it doesn't play nice with the flask_wtf I'm trying to use. Is there a good way to do this?

@app.route('/', methods=['GET', 'POST'])
def upload():
    form = Upload_Form(CombinedMultiDict((request.files, request.form)))
    if form.validate_on_submit():
        files = form.data_file.data
        files_filenames = secure_filename(files.filename)
        data.save(os.path.join(app.config['UPLOAD_FOLDER'], data_filename))
        print(files_filenames)
        return render_template('input_form.html', form=form)
    return render_template('input_form.html', form=form)

class Upload_Form(FlaskForm):
    data_file = FileField(render_kw={'multiple': True}, validators=[FileRequired(), FileAllowed(['txt'], 'text files only')])

<!--input_form.html--->
<form method=post enctype="multipart/form-data">
<table>
    {{ form.hidden_tag() }}
    {% for field in form %}
    <tr>
        <td>{% if field.widget.input_type != 'hidden' %} {{ field.label }} {% endif %}</td><td>{{ field }}</td>
    </tr>
    {% endfor %}
</table>
<p><input type=submit value=Compute></form></p>

这将返回第一个文件,但我需要它来返回所有选定的文件.列表将是最有用的,但是我可以解压缩的任何数据结构都可以使用.谢谢.

This returns the first file but I need it to return all files selected. A list would be most useful but any data structure that I can unpack would work. Thanks.

推荐答案

使用MultipleFileField代替使用FileField.它支持多个文件.

Instead of using the FileField, use the MultipleFileField. It supports multiple files.

例如:

from wtforms import MultipleFileField

class NewFileForm(FlaskForm):
    files = MultipleFileField('File(s) Upload')

然后访问文件:

@app.route('/', methods=['GET', 'POST'])
def upload():
    form = NewFileForm()
    if form.validate_on_submit():
        files_filenames = []
        for file in form.files.data:
            file_filename = secure_filename(file.filename)
            data.save(os.path.join(app.config['UPLOAD_FOLDER'], data_filename))
            files_filenames.append(file_filename)
        print(files_filenames)
        return render_template('input_form.html', form=form)
    return render_template('input_form.html', form=form)

这篇关于如何使用flask-wtf上传多个文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆