Skip to content

Instantly share code, notes, and snippets.

@mseeks
Created February 13, 2024 02:12
Show Gist options
  • Save mseeks/7c802ad35ea6a702877f948dfa3947e3 to your computer and use it in GitHub Desktop.
Save mseeks/7c802ad35ea6a702877f948dfa3947e3 to your computer and use it in GitHub Desktop.
Python Flask snippet for serving a file at an endpoint with a file name derived from an argument passed in, specifically the ID of the file.
from flask import Flask, send_file, request
app = Flask(__name__)
@app.route('/download-file/<file_id>')
def download_file(file_id):
# Construct file path using file ID
file_path = f'path/to/your/files/{file_id}.extension'
# Serve the file for download
return send_file(file_path, as_attachment=True, download_name=f'{file_id}.extension')
if __name__ == '__main__':
app.run(debug=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment