我曾试图上传文件夹,其中包含不同的文件夹中不同的文件。我曾试图做 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>
1条答案
按热度按时间0wi1tuuw1#
您可以使用字典
{'webkitdirectory': True}
添加render_kw
参数。示例:
渲染_kw(字典)
ref
该属性支持平台:
ref1