5

I'm uploading a file to my flask backend and I can't figure out how to access the parameter values in the multipart form.

I can access the uploaded file easily by doing file = request.files['file'] but can't figure out a way to get the parameter values.

I've tried the following but haven't had any luck:

    print(request.data['share'])
    print(request.data['title'])
    print(request.get('share'))
    print(request.get('title'))
Brosef
  • 2,945
  • 6
  • 34
  • 69
  • What parameter values are you expecting? What does your HTML look like? – sytech Oct 11 '16 at 19:29
  • You can refer http://flask.pocoo.org/docs/0.11/patterns/fileuploads/ ...For example, to get the name of file uploaded, you can try 'file.filename'.... – kundan Oct 11 '16 at 19:31
  • @Gator_Python by parameters i mean the (key,value) parameters that are used to construct the multipart form. I'm essentially trying to send data along with the file to my flask app. I want to access that data. – Brosef Oct 11 '16 at 19:31
  • @kundan `file.filename` works, but i'm trying to pass additional data with the file. – Brosef Oct 11 '16 at 19:32
  • How are you passing the arguments ? This will be helpful: http://stackoverflow.com/a/25268170/1840877 – kundan Oct 11 '16 at 19:35

1 Answers1

8

Most form inputs can be retrieved as follows:

request.form.get("fieldname")

Files can be accessed via

request.files.get("fieldname")

Where the fieldnames are the name attribute in the HTML.

Keep in mind that, just because there's a result for request.files.get("someName") doesn't mean a file was actually uploaded. You should check that the filename exists, too, in order to validate if a file was indeed uploaded.

Take for example, the following HTML

<form action="/form_endpoint" method="POST">
  <input type="text" name="data">
  <input type="submit" value="submit">
</form>

You would access the value the user input in the data field by data = request.form.get("data")

sytech
  • 29,298
  • 3
  • 45
  • 86