I've build an rest server in flask with CRUD for Order
objects.
So there's an endpoint for POST /order
to create the Order
and the user can then upload an attachment on POST /order/{order_id}/attachment
I also want to serve the attachment, to be downloadable on the same url.
So that GET /order/{order_id}/attachment
downloads the attachment file.
But when I return the werkzeug.datastructures.FileStorage, with the filename
attribute set, the browser gives me an file to save with name: attachment
Is there a way to send the file with flask, with the correct filename on an different url?
The send_from_directory
method might come in handy to serve the file(s).
Example (a bit pseudo-coded):
@app.route('/order/{order_id}/attachment')
def download_file(order_id):
order = get_order_from_db(order_id)
filename = order.attachment
return send_from_directory(app.config['UPLOAD_FOLDER'], filename)
More on send_from_directory
: http://flask.pocoo.org/docs/0.12/api/#flask.send_from_directory