Solution :
Your error is resulting from the BadRequestKeyError
because of access to the key that doesn't exist in your request.form
.
ipdb> request.form['u_img']
*** BadRequestKeyError: 400 Bad Request: The browser (or proxy) sent a request that this server could not understand.
Uploaded files are keyed under the request.files
and not the request.form
dictionary. Also, you need to use the loop as the value keyed under u_img
is the instance of FileStorage
and not at all iterable.
Please refer below updated code that will help you in resolving all of the issues.
@app.route('/', methods=['GET', 'POST'])
def index():
mytarget = os.path.join(app_root, 'static/img/')
if not os.path.isdir(mytarget):
os.makedirs(mytarget)
if request.method == 'POST':
...
file = request.files['u_img']
file_name = file.myfilename or ''
mydestination = '/'.join([mytarget, file_name])
file.save(mydestination)
...
return render_template('index.html')