我正在尝试使用flask上传多个文件。但是提交表格后我得到的是空名单。 这是我的HTML表单代码:
<form enctype="multipart/form-data" action="/upload" method="post" role="form">
<div class="form-row">
<div class="col-lg-6 form-group">
<input type="text" name="name" class="form-control" id="name" placeholder="Your Name" data-rule="minlen:4" data-msg="Please enter at least 4 chars" required />
<div class="validate"></div>
</div>
<div class="col-lg-6 form-group">
<input type="email" class="form-control" name="email" id="email" placeholder="Your Email" data-rule="email" data-msg="Please enter a valid email" required />
<div class="validate"></div>
</div>
</div>
<div class="form-group">
<input type="tel" class="form-control" name="phone" id="subject" placeholder="Phone Number" data-rule="minlen:4" data-msg="Please enter your phone number" required />
<div class="validate"></div>
</div>
<div class="form-group">
<select name="cata" class="browser-default custom-select">
<option selected>Culture, Arts and Social Scineces</option>
<option value="1">Medicine and Health</option>
<option value="2">Leadership, Managment, Buisness and Commerce</option>
<option value="3">Sceince, Agriculture and Engineering</option>
<option value="3">Other</option>
</select>
</div>
<div class="form-group">
<textarea class="form-control-a" name="message" rows="5" data-rule="required" data-msg="Please write something for us" placeholder="Message"></textarea>
<div class="validate"></div>
</div>
<div class="mb-3">
<!-- <div class="loading">Loading</div>
<div class="error-message"></div>
<div class="sent-message">Your message has been sent. Thank you!</div> -->
</div>
<strong>Upload File:
<input type="file" name="atta_file[]" multiple>
</strong>
<input type="submit" style="border-radius: 5px;
width: 110px;
margin: 14px;
height: 50px;
line-height: 0;"
class="btn btn-primary pull-right"
value="Submit">
</form>
这是我用于上传文件的Python脚本:
@app.route("/upload", methods=['POST','GET'])
def upload():
if request.method == 'POST':
filess = request.files.getlist('atta_file[]')
print(filess)
return render_template("upload.html")
return render_template("upload.html")
最初,我尝试在控制台上打印提交的文件名,以查看其是否正常运行。 我也针对相同的查询寻找其他答案,但发现在他们的问题中,他们仅在中缺少 enctype =“ multipart / form-data” 或 name 字段>输入标签。但是我已经检查了我的代码,没有一个丢失。我不确定我缺少什么以及如何解决此问题。
答案 0 :(得分:0)
我不知道您是否仍需要帮助。 但是我有同样的问题,我想我找到了解决方案。 好吧,就我而言,我正在使用ajax请求来发送文件。
我意识到,当我索引用于发送文件的var时,在python中我只有一个文件,而我发现了这个example。
因此,以本示例为参考。
JavaScript代码:
var form_data = new FormData();
var auxnames = "";
var wordsF = $("#word").prop("files");
$.each(wordsF, function(i, file){
form_data.append(file.name, file);
auxnames += file.name+',';
});
form_data.append('auxindex', auxnames);
$.ajax({
url: $SCRIPT_ROOT + "/upload_files",
type:"post",
data:form_data,
contentType: false,
cache: false,
processData: false,
}).done(function(response){ ...
Python代码:
if request.method == 'POST':
auxindex = request.form['auxindex']
auxindex = auxindex.split(",")
auxindex = list(filter(None, auxindex))
for file in auxindex:
auxfile = request.files[file]
namefile = secure_filename(file)
auxfile.save(os.path.join(path, namefile))
也许不是最好的方法,但是它对我有用,我希望这对您有用。