I'm running into an oddity that I can't explain, other than it's an inline, instead an attachment.
def respond_as_inline(request, file_path, original_filename, ranged=False):
# https://stackoverflow.com/questions/36392510/django-download-a-file
# https://stackoverflow.com/questions/27712778/video-plays-in-other-browsers-but-not-safari
# https://stackoverflow.com/questions/720419/how-can-i-find-out-whether-a-server-supports-the-range-header
filename = os.path.join(file_path, original_filename)
if os.path.exists(filename):
mtype, encoding = mimetypes.guess_type(original_filename)
if mtype is None:
mtype = 'application/octet-stream'
with open(filename, 'rb') as fh:
if ranged:
response = RangedFileResponse(request, file=open(filename, 'rb'), as_attachment=False, filename=original_filename)
response["Content-Type"] = mtype
else:
response = HttpResponse(fh.read(), content_type=mtype)
response['Content-Disposition'] = 'inline; filename=%s'% original_filename
return response
else:
print("File not found")
raise Http404
Now this is working fine, except for the fact, that if I use the inline attachment, the filename that is shown from the browser is the url name (a UUID), and not the filename (eg original_filename).
If I change this to be an attachment, when it is downloaded, the correct filename is detected/used.
Anyone have a good answer/solution?
And no, by design this is inline, so that the PDF, graphic, etc, can be viewed in the browser. If I can it to be an attachment, the default is to save to disk. That's not helpful on an tablet.