无法为 xml 下载创建 HttpResponse,Django
Trouble creating HttpResponse for xml download, Django
我正在尝试让用户下载我生成的 xml 文件。
这是我的代码:
tree.write('output.xml', encoding="utf-16")
# Pathout is the path to the output.xml
xmlFile = open(pathout, 'r')
myfile = FileWrapper(xmlFile.read())
response = HttpResponse(myfile, content_type='application/xml')
response['Content-Disposition'] = 'attachment; filename='+filename
return response
当我尝试创建我的响应时,我得到了这个异常:
'\'str\' object has no attribute \'read\''
不知道我做错了什么。有什么想法吗?
编辑:
当我使用这段代码时,我没有收到任何错误,但下载的文件是空的
tree.write('output.xml', encoding="utf-16")
xmlFile = open(pathout, 'r')
myfile = FileWrapper(xmlFile)
response = HttpResponse(myfile, content_type='application/xml')
response['Content-Disposition'] = 'attachment; filename='+filename
return response
您正在读取文件并将生成的字符串传递给 FileWrapper,而不是传递实际的文件对象。
myfile = FileWrapper(xmlFile)
您正在调用 xmlFile.read()
- 它会产生一个字符串 - 并将结果传递给 FileWrapper()
,它需要一个可读的类文件对象。您应该将 xmlFile
传递给 FileWrapper
,或者根本不使用 FileWrapper
并将 xmlFile.read()
的结果作为您的 HttpResponse
正文传递。
请注意,如果您正在动态创建 xml(根据您的代码片段的第一行,情况似乎就是如此),将其写入磁盘只是为了在几行之后读回它是一种浪费时间和资源以及竞争条件的潜在原因。你也许想看看 https://docs.python.org/2/library/xml.etree.elementtree.html#xml.etree.ElementTree.tostring
或者从其他答案中,我建议使用 Django 模板系统完全解决这个问题:
from django.http import HttpResponse
from django.template import Context, loader
def my_view(request):
# View code here...
t = loader.get_template('myapp/myfile.xml')
c = Context({'foo': 'bar'})
response = HttpResponse(t.render(c), content_type="application/xml")
response['Content-Disposition'] = 'attachment; filename=...'
return response
以这种方式创建一个 myfile.xml
模板,用于呈现正确的 xml 响应,而无需处理将任何文件写入文件系统。这更干净、更快,因为 确实不需要创建 xml 并永久存储它。
我正在尝试让用户下载我生成的 xml 文件。
这是我的代码:
tree.write('output.xml', encoding="utf-16")
# Pathout is the path to the output.xml
xmlFile = open(pathout, 'r')
myfile = FileWrapper(xmlFile.read())
response = HttpResponse(myfile, content_type='application/xml')
response['Content-Disposition'] = 'attachment; filename='+filename
return response
当我尝试创建我的响应时,我得到了这个异常:
'\'str\' object has no attribute \'read\''
不知道我做错了什么。有什么想法吗?
编辑: 当我使用这段代码时,我没有收到任何错误,但下载的文件是空的
tree.write('output.xml', encoding="utf-16")
xmlFile = open(pathout, 'r')
myfile = FileWrapper(xmlFile)
response = HttpResponse(myfile, content_type='application/xml')
response['Content-Disposition'] = 'attachment; filename='+filename
return response
您正在读取文件并将生成的字符串传递给 FileWrapper,而不是传递实际的文件对象。
myfile = FileWrapper(xmlFile)
您正在调用 xmlFile.read()
- 它会产生一个字符串 - 并将结果传递给 FileWrapper()
,它需要一个可读的类文件对象。您应该将 xmlFile
传递给 FileWrapper
,或者根本不使用 FileWrapper
并将 xmlFile.read()
的结果作为您的 HttpResponse
正文传递。
请注意,如果您正在动态创建 xml(根据您的代码片段的第一行,情况似乎就是如此),将其写入磁盘只是为了在几行之后读回它是一种浪费时间和资源以及竞争条件的潜在原因。你也许想看看 https://docs.python.org/2/library/xml.etree.elementtree.html#xml.etree.ElementTree.tostring
或者从其他答案中,我建议使用 Django 模板系统完全解决这个问题:
from django.http import HttpResponse
from django.template import Context, loader
def my_view(request):
# View code here...
t = loader.get_template('myapp/myfile.xml')
c = Context({'foo': 'bar'})
response = HttpResponse(t.render(c), content_type="application/xml")
response['Content-Disposition'] = 'attachment; filename=...'
return response
以这种方式创建一个 myfile.xml
模板,用于呈现正确的 xml 响应,而无需处理将任何文件写入文件系统。这更干净、更快,因为 确实不需要创建 xml 并永久存储它。