def download():
trackrels = request.query.tracks.split('|')
# write the archive into a temporary in-memory file-like object
temp = tempfile.SpooledTemporaryFile()
with zipfile.ZipFile(temp, 'w', zipfile.ZIP_DEFLATED) as archive:
for trackrel in trackrels:
base_wildcard = trackrel.replace("-track.csv", "*")
paths = config.TRACKDIR.glob(base_wildcard)
for path in paths:
archive.write(str(path),
str(path.relative_to(config.TRACKDIR))
)
temp.seek(0)
# force a download; give it a filename and mime type
response.set_header('Content-Disposition', 'attachment; filename="data.zip"')
response.set_header('Content-Type', 'application/zip')
# relying on garbage collector to delete tempfile object
# (and hence the file itself) when done sending
return temp
评论列表
文章目录