1

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.

1 Answers1

1

Okay, I suspected this, but technically an inline "attachment", while containing a filename in the header, is seen by the browser as being from the URL. So the URL is effectively (imho) overriding the filename.

So this technically is not an Django issue, this is a web browser issue.

This StackOverflow thread:

Set Title of Inline File?

Put me on the "right" track...

Instead of http://servername/download/(uuid)

I've rewritten the URL to be:

http://servername/download/filename?UUID=(uuid)

The filename is displayed in the browser, but is not actually used by Django. The UUID is the only data that is used by the server. So cosmetically this solves the issue...

I'm not in love with this solution, but it's simple, and works...

But if anyone has an more elegant solution, or feels like pointing out some issues, please feel free...