【问题标题】:How to serve non ascii file name with django and apache using xsendfile如何使用 xsendfile 为 django 和 apache 提供非 ascii 文件名
【发布时间】:2021-11-27 14:35:25
【问题描述】:

我想在 django 中提供受保护的内容,所以我尝试在 django 和 apache 中设置 xsendfile。它适用于 ascii 文件名,但是当我尝试使用非 ascii 文件名时,apache 服务器以 404 响应。有人知道这里发生了什么吗?

Django:
def media_xsendfile_pdf(request):
file = Media.objects.last()
response = HttpResponse()
response['Content-Type'] = 'application/pdf'
response['X-Sendfile']= smart_str(f"{settings.BASE_DIR}{file.file.url}")
return response

Apache:
AllowEncodedSlashes On
XSendFile On
XsendFilePath /path-to-the-protected-resource

【问题讨论】:

    标签: python python-3.x django django-views x-sendfile


    【解决方案1】:

    我能找到解决办法,

    在响应头中,我们需要如下设置X-sendfile

    from urllib.parse import unquote
    response["X-sendfile"] = unquote(<url>).encode('utf-8')
    

    我希望以上内容对坚持这一点的人有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-10-08
      • 1970-01-01
      • 1970-01-01
      • 2014-02-20
      • 1970-01-01
      • 2015-02-12
      • 2017-01-24
      • 2017-04-27
      相关资源
      最近更新 更多