gpt4 book ai didi

Python - Flask 和 werkzeug - 继续提供 "BadRequestKeyError: 400 Bad Request: KeyError: ' 文件'"

转载 作者:太空宇宙 更新时间:2023-11-03 21:19:29 25 4
gpt4 key购买 nike

我第一次尝试使用python上传文件,我尝试使用flask和werkzeug库,这是我的代码:

这里我正在创建上传文件的函数:

@app.route('/upload')
def upload_file():
return render_template('load.html')

@app.route('/uploader', methods=['GET','POST'])
@login_required(must=[be_admin, have_approval])
def uploaderV():
if request.method == 'POST':
file = request.files['file']
if file:
filename = secure_filename(file.filename)
file.save(os.path.join(app.config['UPLOAD_FOLDER'],filename))
return 'file uploaded'
return render_template('load.html')

这是我的 load.html 页面:

{% extends 'base.html' %}
{% block title %}Secret{% endblock %}
{% block page_body %}
<div class="row">
<form action="{{ url_for('uploaderV') }}" method="POST" enctype="multipart/form-data">
<p>
<input type='file' name='file[]' multiple=''>
<input type="submit" value="Upload">
</p>
</form>
</div>
{% endblock %}

每次我尝试上传文件时,服务器都会给我
werkzeug.exceptions.BadRequestKeyErrorBadRequestKeyError:400错误请求:KeyError:'文件'
我尝试了不同的方法,但现在我真的不知道该怎么办。

最佳答案

您的输入名称是 file[] ,而不是 file 。尝试这样的事情:

   file = request.files['file[]']

或者只需将输入名称更改为file

关于Python - Flask 和 werkzeug - 继续提供 "BadRequestKeyError: 400 Bad Request: KeyError: ' 文件'",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54418594/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com