Download the file, analyze it and submit to Flask

I am taking the first steps with Flask. I can successfully download the file from the client and return it with the code from here: http://flask.pocoo.org/docs/patterns/fileuploads/

But how to change it (for example, line by line), and then transfer it to the client?

I can get the line with read() after:

 if file and allowed_file(file.filename): 

and then process it. So, the real question is: how can I serve the output string as a file?

I do not want to save it at all on the hdd server (both the original version and the modified one).

+6
source share
1 answer

You can use make_response to create an answer for your line and add Content-Disposition: attachment; filename=anyNameHere.txt Content-Disposition: attachment; filename=anyNameHere.txt to it before returning it:

 @app.route("/transform-file", methods=["POST"]) def transform(): # Check for valid file and assign it to `inbound_file` data = inbound_file.read() data = data.replace("A", "Z") response = make_response(data) response.headers["Content-Disposition"] = "attachment; filename=outbound.txt" return response 

See also: Documents in Streaming Content

+7
source

Source: https://habr.com/ru/post/943713/


All Articles