我有一个Django视图,它返回一个带有特殊MIME类型的 HttpResponse ,以使用户的浏览器“下载”。该文件而不是在浏览器中查看它。问题是响应将保存的默认文件名是用户尝试访问的URL。

有没有办法在 HttpResponse 对象中包含默认文件名,还是我必须重定向到另一个URL才能实现?

有帮助吗?

解决方案

docs 中有一个相关示例:

from django.http import HttpResponse

def some_view(request):
    # Create the HttpResponse object with the appropriate headers.
    response = HttpResponse(mimetype='application/pdf', <snip>)
    response['Content-Disposition'] = 'attachment; filename="somefilename.pdf"'
    return response
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top