Extract file from file storage object in flask

dhinar picture dhinar · Apr 17, 2018 · Viewed 14.8k times · Source

I am learning how to work with file uploads in flask. Earlier from here, i worked with pdf file uploads and read the contents of it. This happens inside client.py file.

Now i would like to pass my file from client to server that is running locally. When i use request.file, it will get it as FileStorage Object. So, without saving or providing file path, i want to upload file from client and pass it to the server to do further process.

class mainSessRunning():
     def proces(input):
     ...
     ...
     return result

run = mainSessRunning()

@app.route('/input', methods=['POST'])
def input():
    input_file = request.files['file']
   ...(extract file from filestorage object "input_file")...
    result = run.process(file) ## process is user defined function 
    return (result)

here i want to send the incoming file through process() function to server running locally. How do i do this? I came across same question but couldn't able to find anything

Answer

Sraw picture Sraw · Apr 17, 2018

What do you mean "extract"? If you want get the bytes of file, you can use content = request.files['file'].read().

And then send this content to any where you want: res = requests.post(url, content)