试图上传并保存通过Flask上传的文件。继续出现400错误,文件永不保存。
我的路线文件具有以下代码:
@app.route('/handle_date', methods=['GET', 'POST'])
def handle_data():
file = request.files['file']
# storing uploaded file so that oce_test.py can access it
filename = secure_filename(file.filename)
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
return redirect('/index')
我的表格如下:
<html>
<head>
<title>OCE Demo</title>
</head>
<body>
<form action="{{ url_for('handle_data') }}" method="POST">
<h2>OCE Input Form</h2>
<label for="input">Input Selection </label>
<select name="input">
<option value="price">24 hr Prices</option>
<option value="dispatch">24 hr Dispatch</option>
</select>
<br>
<br>
<br>
<label>Select a file:</label>
<input type="file" name="file"><br><br>
<input type="submit">
</form>
</body>
</html>
我的踪迹如下:
答案 0 :(得分:1)
您的表单缺少enctype="multipart/form-data"
。应该是
<form action="{{ url_for('handle_data') }}" method="POST" enctype="multipart/form-data">