我在Chrome浏览器中收到ERR_RESPONSE_HEADERS_MULTIPLE_CONTENT_DISPOSITION,并在django中使用以下给定代码。
redirect_path = 'some-url'
response = HttpResponseRedirect(redirect_path)
response['Content-Disposition'] = 'attachment; filename=file-from-+324#10,+4324.mp3'
return response
在其他浏览器中找不到东西。
请让我知道我在这方面做错了什么。
答案 0 :(得分:4)
问题是文件名中有逗号。你应该在文件名中引用标题。我测试了以下视图在Chrome中的效果。
def my_view(request):
response = HttpResponse('hello')
response['Content-Disposition'] = 'attachment; filename="filename,with,commas.txt"'
return response
有关详细信息,请参阅Chrome帮助论坛上的this discussion。
答案 1 :(得分:0)
除了@Alasdair的答案,如果它是动态生成的,从文件名中删除逗号,你可以在.replace()
对象中构造文件名时调用response
。
redirect_path = 'some-url'
response = HttpResponseRedirect(redirect_path)
response['Content-Disposition'] = 'attachment; filename=file-from-+324#10,+4324.mp3'
return response
变为
redirect_path = 'some-url'
filename_to_return = "file-from-+324#10,+4324.mp3".replace(',', '_')
response = HttpResponseRedirect(redirect_path)
response['Content-Disposition'] = 'attachment; filename=%s' % filename_to_return
return response
您只需将文件名字符串替换为传递给函数的动态变量。