mirror of
https://github.com/inventree/InvenTree.git
synced 2026-11-25 09:13:08 +00:00
Provide download link to export BOM file
- Helper function for generating temporary file (in memory) for exporting
This commit is contained in:
1 parent
b58c49e066
commit
b8e28c003d
3 files changed
+57
-24
No files matched your search
@@ -0,0 +1,32 @@
|
||||
import io
|
||||
import os
|
||||
from wsgiref.util import FileWrapper
|
||||
from django.http import StreamingHttpResponse, HttpResponse
|
||||
|
||||
|
||||
def WrapWithQuotes(text):
|
||||
# TODO - Make this better
|
||||
if not text.startswith('"'):
|
||||
text = '"' + text
|
||||
|
||||
if not text.endswith('"'):
|
||||
text = text + '"'
|
||||
|
||||
return text
|
||||
|
||||
|
||||
def DownloadFile(data, filename, content_type='application/text'):
|
||||
"""
|
||||
Create a dynamic file for the user to download.
|
||||
@param data is the raw file data
|
||||
"""
|
||||
|
||||
filename = WrapWithQuotes(filename)
|
||||
|
||||
wrapper = FileWrapper(io.StringIO(data))
|
||||
|
||||
response = StreamingHttpResponse(wrapper, content_type=content_type)
|
||||
response['Content-Length'] = len(data)
|
||||
response['Content-Disposition'] = 'attachment; filename={f}'.format(f=filename)
|
||||
|
||||
return response
|
||||
Reference in new issue
Block a user