问题
@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
回答1:
In your html form tag include
<form action="/path" method="post" enctype="multipart/form-data">
</form>
来源:https://stackoverflow.com/questions/47679398/file-upload-error-in-flask