【发布时间】:2020-05-26 19:07:30
【问题描述】:
我需要创建一个可以通过 Flask 存储整个文件夹、文件夹或文件的实用程序,目前我正在使用以下代码一次上传 1 个文件
烧瓶代码:
from flask import Flask, render_template, request
from werkzeug.utils import secure_filename
app = Flask(__name__)
@app.route('/')
def upload_file():
return render_template('upload.html')
@app.route('/uploader', methods = ['GET', 'POST'])
def upload_file1():
if request.method == 'POST':
f = request.files['file']
f.save(secure_filename(f.filename))
return 'file uploaded successfully'
if __name__ == '__main__':
app.run(debug = True)
html代码:
<html>
<body>
<form action = "http://localhost:5000/uploader" method = "POST"
enctype = "multipart/form-data">
<input type = "file" name = "file" />
<input type = "submit"/>
</form>
</body>
</html>
我收到以下对话框:
但这只允许我一次插入一个文件。
我需要一些可以让我上传整个目录或多个文件的东西。可以使用 Flask 完成吗?如果没有,在 Python 中有没有可用的替代方法?对于此事的任何帮助将不胜感激!
【问题讨论】:
-
这能回答你的问题吗? Uploading multiple files with Flask
-
不,我已经完成了该线程,但对我没有任何帮助。
-
您是否仔细研究并比较了您提供的代码与链接线程中的代码的差异,例如在HTML中包含
multipleattribute in the<input>tag which is needed to permit selection of multiple files?您是否注意到您可能需要调查getlist的使用情况? -
哦!所以这就是我所缺少的。对不起!我太粗心了:(
标签: python file flask file-upload web-applications