Created
December 14, 2021 04:28
-
-
Save devpilot/1a0a478f12405cf79f735ac3b1e7915e to your computer and use it in GitHub Desktop.
Demo api for zip download
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, make_response | |
import zipfile | |
import io | |
import pathlib | |
app = Flask(__name__) | |
@app.route("/download",methods=['GET']) | |
def request_zip(): | |
base_path = pathlib.Path('./data/') | |
data = io.BytesIO() | |
with zipfile.ZipFile(data, mode='w') as z: | |
for f_name in base_path.iterdir(): | |
z.write(f_name) | |
data.seek(0) | |
response = make_response( | |
send_file( | |
data, | |
mimetype='application/zip', | |
as_attachment=True, | |
attachment_filename='logs.zip' | |
) | |
) | |
response.headers['Access-Control-Allow-Origin'] = '*' | |
return response | |
if __name__ == '__main__': | |
app.run(host='0.0.0.0',port = 8090, threaded = True, debug = True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment