【问题标题】:Django Downloaded File NameDjango 下载的文件名
【发布时间】:2013-12-10 10:44:22
【问题描述】:

我使用一个简单的视图类通过它的 pk 来“强制下载”一个文件。下载的文件名称是 pk。我希望它是原始文件名,就像它在服务器上一样。 我该怎么做?

*我是 django 新手

这里是视图的代码

class GetFileContent(View):
def get(self,request, userName, pk):
    user = User.objects.filter(username = userName)
    filtered = File.objects.filter(pk = pk, published=True, file_owner = user)
    data = filtered[0].content
    return HttpResponse(data, content_type='application/force-download')
    pass

【问题讨论】:

标签: python django view download


【解决方案1】:

您需要在响应中设置文件名:

response = HttpResponse(data, content_type='application/force-download')
response['Content-Disposition'] = 'attachment; filename="{}"'.format(filename)
return response

您需要找到一种方法来确定所需的文件名(例如,将其存储在数据库中以供查找)。

【讨论】:

    猜你喜欢
    • 2021-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-16
    • 2012-06-15
    • 1970-01-01
    相关资源
    最近更新 更多