python - Python - 烧瓶和 werkzeug - 继续给出“BadRequestKeyError:400 Bad Request:KeyError:'file'”
问题描述
我第一次尝试用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.BadRequestKeyError BadRequestKeyError: 400 Bad Request: KeyError: 'file'
我尝试了不同的方式,现在我真的不知道该怎么做。
解决方案
你输入的名字是file[]
,不是file
。尝试类似:
file = request.files['file[]']
或者只是将您的输入名称更改为file
.
推荐阅读
- python - 启动 Anaconda Prompt 时缺少指定的配置类型
- python - R 和 Python 中概率之间的差异 (XGBoost)
- git - git“在 X 提交之后或之前”的真正含义是什么?
- java - 如何获取项目的位置并使用光标将其删除?
- javascript - Javascript Fetch - 当 ok = false 时从服务器获取数据
- regex - 如何在 python 中匹配只有一个符号 + 或 - 而不是 +- 或 -+ 的数字 - HackerRank 挑战
- xml - `-:55: HTML parser error : htmlParseEntityRef: expecting ';'`: 用 xmllint 清理 HTML 文件?
- java - 使用 FK“重用”保存实体
- php - .Htaccess 删除 .php 和 url 重写不能一起工作
- sqlalchemy - 有没有办法为引擎建立的所有连接设置自动提交为真?