Created
February 13, 2024 02:12
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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