简体   繁体   English

Django和/或Apache:使用不同的名称提供(静态)文件

[英]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 . 假设用户导航到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 . 在该URL的Django视图中,我现在可以查找对应文件,该文件可能位于服务器上.../files/46dfa12bbf32d523fbb3642dfee45bb4

So how can I now get this file to be served as some.pdf to the client? 那么我现在如何才能将此文件作为some.pdf给客户端? 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. 我也不知道在什么级别(Apache或Django)这种类型的操作最好处理。 But since I didn't find anything about this for Apache or Django, I would be interested in solutions for either of them. 但是由于我没有为Apache或Django找到任何关于它的内容,我会对其中任何一个的解决方案感兴趣。

Quoting django docs Telling the browser to treat the response as a file attachmen , you should inform it in response: 引用django文档告诉浏览器将响应视为文件附件 ,您应该通知它作为响应:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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