0

I'm not able to upload files through a simple form, using Flask.

I wrote below the simplified code of the html:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Debug</title>
</head>
<body>
   <form id="form_id" target="" action="" enctype="multipart/form-data" method = "POST">
     <input type="file" name="file">
     <button type="submit" name="action" value="add">Upload</button>
   </form>
</body>
</html>

And the Python Flask backend:

from flask import Flask, request, render_template

app = Flask(__name__)
app.config['SECRET_KEY'] = 'JUST_FOR_TEST'

@app.route("/", methods=['GET', 'POST'])
def debugger():
    args = request.args.to_dict()
    if args != {}:
        return str(args)
    else:
        return render_template('debug.html')

if __name__ == "__main__":
    app.run(debug=True)

What should I fix for this form to work? Thank you all for your support.

  • the form works fine. what do you want to happen when the user clicks `submit`? do you want to save the file? – AudioBaton Aug 17 '22 at 19:14
  • I would link to see a key "file" in "args" / request. But it seems that the form does not submit any "file" key in the request. – André Cerutti Franciscatto Aug 17 '22 at 19:47
  • isn't `request.args` used for URL queries and `request.form` used for HTML forms? [here](https://stackoverflow.com/questions/34671217/in-flask-what-is-request-args-and-how-is-it-used) – AudioBaton Aug 17 '22 at 20:04
  • Thank you, AudioBaton. Changing request.args to request.form still returns an empty request: "ImmutableMultiDict([])" The problem seems to be in the html form. For some reason it does not append the file in the request. And doesn't metter what extension of file is attached. – André Cerutti Franciscatto Aug 17 '22 at 20:07

1 Answers1

1

The clue provided by @AudioBaton solved the problem.

to access the file in request, one needs to pass

request.files['the_value_name_of_the_field_in_the_form']

instead of request.args or request.form.

Thank you, AudioBaton.