作者:奔跑的人儿 | 来源:互联网 | 2022-12-28 11:54
1> Oluwafemi Su..:
BadRequestKeyError
由于访问不存在的密钥而导致的错误request.form
.
ipdb> request.form['u_img']
*** BadRequestKeyError: 400 Bad Request: The browser (or proxy) sent a request that this server could not understand.
上传的文件是键入的request.files
而不是request.form
字典.此外,您需要丢失循环,因为键入的值u_img
是一个实例FileStorage
而不是可迭代的.
@app.route('/', methods=['GET', 'POST'])
def index():
target = os.path.join(app_root, 'static/img/')
if not os.path.isdir(target):
os.makedirs(target)
if request.method == 'POST':
...
file = request.files['u_img']
file_name = file.filename or ''
destination = '/'.join([target, file_name])
file.save(destination)
...
return render_template('index.html')