简体   繁体   中英

Django and/or Apache: serve (static) files with a different name

I store a lot of files, using a hash as file name, but when a user wants to download it, I would like to be able to give the file a new name.

Let's say the user navigates to www.myurl.com/userxy/files/some.pdf . In the Django View for that url, I can now look up the correspondig file, which might be on the server on .../files/46dfa12bbf32d523fbb3642dfee45bb4 .

So how can I now get this file to be served as some.pdf to the client? Do I have to copy the file first, and giving it a different name on the disc or can I somehow serve the original file? Is what I'm trying at all good practice?

I also don't know on what level (Apache or Django) this type of operation is best handled. But since I didn't find anything about this for Apache or Django, I would be interested in solutions for either of them.

Quoting django docs Telling the browser to treat the response as a file attachmen , you should inform it in response:

>>> response = HttpResponse(my_data, content_type='application/vnd.ms-excel')
>>> response['Content-Disposition'] = 'attachment; filename="some.pdf"'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM