Allow downloaded files to be inline or attachments

This commit is contained in:
Oliver Walters 2021-08-07 21:45:18 +10:00
parent 512eceb2a6
commit 174ac64235

View File

@ -344,13 +344,15 @@ def GetExportFormats():
] ]
def DownloadFile(data, filename, content_type='application/text'): def DownloadFile(data, filename, content_type='application/text', inline=False):
""" Create a dynamic file for the user to download. """
Create a dynamic file for the user to download.
Args: Args:
data: Raw file data (string or bytes) data: Raw file data (string or bytes)
filename: Filename for the file download filename: Filename for the file download
content_type: Content type for the download content_type: Content type for the download
inline: Download "inline" or as attachment? (Default = attachment)
Return: Return:
A StreamingHttpResponse object wrapping the supplied data A StreamingHttpResponse object wrapping the supplied data
@ -365,7 +367,10 @@ def DownloadFile(data, filename, content_type='application/text'):
response = StreamingHttpResponse(wrapper, content_type=content_type) response = StreamingHttpResponse(wrapper, content_type=content_type)
response['Content-Length'] = len(data) response['Content-Length'] = len(data)
response['Content-Disposition'] = 'attachment; filename={f}'.format(f=filename)
disposition = "inline" if inline else "attachment"
response['Content-Disposition'] = f'{disposition}; filename={filename}'
return response return response