File upload error in flask

可紊 提交于 2021-02-08 06:35:35

问题


@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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!