The problem is I am unable to send headers when using send_file
, is there any workaround for this?
I want to be able to send headers when sending files to client.
return send_file(mp3_filepath, 'audio/mpeg')
To set response headers in Flask and Python, we set the headers property of the response object. to call make_response to create response object that returns a string response. Finally, we return the resp object in the home route.
Flask – File Uploading Handling file upload in Flask is very easy. It needs an HTML form with its enctype attribute set to 'multipart/form-data', posting the file to a URL. The URL handler fetches file from request. files[] object and saves it to the desired location.
In order to offer several files together as a download, you only have the option of compressing them in an archive. In my example, all files that match the specified pattern are listed and compressed in a zip archive. This is written to the memory and sent by the server.
from flask import make_response, send_file response = make_response(send_file(mp3_filepath)) response.headers['X-Something'] = 'header value goes here' return response
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With