0

I was able to upload large file to server using the below code -

@app.route("/upload", methods=["POST"])
def upload():
    with open("/tmp/output_file", "bw") as f:
        chunk_size = 4096
        while True:
            chunk = request.stream.read(chunk_size)
            if len(chunk) == 0:
                return
            f.write(chunk)

But if I use request.form['userId'] or any parameter which is sent as form data in the above code it fails.

As per one of the blog post it says- Flask’s request has a stream, that will have the file data you are uploading. You can read from it treating it as a file-like object. The trick seems to be that you shouldn’t use other request attributes like request.form or request.file because this will materialize the stream into memory/file. Flask by default saves files to disk if they exceed 500Kb, so don’t touch file.

Is there a way where we can send additional parameters like userId along with the file being uploaded in flask?

SkyTreasure
  • 854
  • 2
  • 13
  • 23

1 Answers1

0

use headers in requests.

if you want to send user name along with data

headers['username'] = 'name of the user'
r = requests.post(url, data=chunk, headers=headers)