AlgorithimicDUMB
AlgorithimicDUMB

Reputation: 85

File upload error in flask

@app.route('/registerdriver', methods=['POST'])
def register_driver():
    fname = request.form['fname']
    lname = request.form['lname']
    email = request.form['email']
    mobno = request.form['mobno']
    password = request.form['password']

    file = request.files['driving_license']
    file.filename = mobno+"_"+fname

    filename = secure_filename(file.filename)
    file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))

Above is the code I used for saving the file. However the following error pops out while trying to save the file

flask.debughelpers.DebugFilesKeyError

flask.debughelpers.DebugFilesKeyError: You tried to access the file "driving_license" in the request.files dictionary but it does not exist. The mimetype for the request is "application/x-www-form-urlencoded" instead of "multipart/form-data" which means that no file contents were transmitted. To fix this error you should provide enctype="multipart/form-data" in your form.

The browser instead transmitted some file names.

Can someone help me with this

Upvotes: 5

Views: 8992

Answers (2)

Anas Limem
Anas Limem

Reputation: 1

file = request.files.get('driving_license')

Try this because i had the same error even when i already have <form action="/path" method="post" enctype="multipart/form-data"> so now it should works with that

Upvotes: 0

Raj
Raj

Reputation: 183

In your html form tag include

<form action="/path" method="post" enctype="multipart/form-data">
</form>

Upvotes: 10

Related Questions