html 如何使用flask和wtforms上载文件夹目录

stszievb  于 2023-03-21  发布在  其他
关注(0)|答案(1)|浏览(166)

我曾试图上传文件夹,其中包含不同的文件夹中不同的文件。我曾试图做 flask ,我找不到。请帮助我上传特定目录中的文件夹
请帮我上传文件夹在特定的目录与 flask 和忍者

from flask import Flask, render_template,redirect,request,url_for
from flask_wtf import FlaskForm
from wtforms import FileField, SubmitField
from werkzeug.utils import secure_filename
import os

app = Flask(__name__)
app.config['SECRET_KEY'] = 'supersecretkey'
app.config['UPLOAD_FOLDER'] = 'static/files'

class UploadFileForm(FlaskForm):
    file = FileField("File", validators=[InputRequired()])
    submit = SubmitField("Upload File")

@app.route('/', methods=['GET',"POST"])
@app.route('/home', methods=['GET',"POST"])
def home():
    form = UploadFileForm()
    if form.validate_on_submit():
        file = form.file.data # First grab the file
        file.save(os.path.join(os.path.abspath(os.path.dirname(__file__)),app.config['UPLOAD_FOLDER'],secure_filename(file.filename))) # Then save the file
        return "File has been uploaded."
    return render_template('index.html', form=form)

if __name__ == '__main__':
    app.run(debug=True)

HTML页面

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Home</title>
</head>
<body>
    <h1>Flask File Uploads Tutorial</h1>

    <form method='POST' enctype='multipart/form-data'>
        {{form.hidden_tag()}}
        {{form.file()}}
        {{form.submit()}}
    </form>
</body>
</html>
0wi1tuuw

0wi1tuuw1#

您可以使用字典{'webkitdirectory': True}添加render_kw参数。
示例:

img_dataset = FileField('img_dataset', validators=[FileRequired(),
    FileAllowed(images, 'Please select image dataset')], render_kw={'webkitdirectory': True})

渲染_kw(字典)

  • 一个字典,它提供默认关键字,这些关键字将在呈现时提供给小部件。
    ref
    该属性支持平台:

    ref1

相关问题