Python 代码
from flask import Flask, app, render_template, request
app = Flask(__name__)
app.config['SEND_FILE_MAX_AGE_DEFAULT'] = 1
@app.route('/')
def index():
return render_template('index.html')
@app.route('/after', methods=['GET', 'POST'])
def after():
file = request.files['file1']
file.save('static/file.jpg')
return render_template('predict.html')
if __name__ == '__main__':
app.run(debug = True)
以下是来自'index.html'的html代码:
<html>
<form action="{{url_for('after')}}">
<input type= "submit" enctype = 'multipart/form-data' method ="POST">
<input type="file" name='file1'>
</form>
</html>
接下来是'predict.html'中的代码
<html>
<body>
<img src="{{url_for('static', filename = 'file.jpg')" alt="">
</body>
</html>
我试图通过单击“选择文件”按钮来选择一个图像文件以提交它,但在执行此操作时出现以下错误;
werkzeug.exceptions.BadRequestKeyError: 400 Bad Request: 浏览器(或代理)发送了一个该服务器无法理解的请求。 密钥错误:“文件 1”
答案 0 :(得分:0)
为您的 enctype
标签设置 method
和 form
属性,而不是为 input
设置:
<html>
<form action="{{url_for('after')}}" enctype="multipart/form-data" method="POST">
<input type="submit">
<input type="file" name="file1">
</form>
</html>