Python 3:使用内存Xml创建内存Zipfile

问题描述

我正在尝试使用io.StringIO()创建XML文件并将其打包到ZipFile中,但是输出一个空的zip文件。哪里有错?

string_xml_buffer = io.StringIO()
string_xml_buffer.write('<MyContent>')
string_xml_buffer.write('</MyContent>')
bytes_zip_buffer = io.BytesIO()
zf = ZipFile(bytes_zip_buffer,mode = 'w')
zf.writestr('filename.xml',string_xml_buffer.getvalue())

# Django response
response = HttpResponse(zf,content_type='application/zip')
response['Content-disposition'] = 'attachment; filename="f.zip"'
return response

解决方法

问题出在缺少 <div className="row"> { //Check if button clicked (this.state.clicked) ? <CommonTableForm someDataPassHere={data}/> : null } </div> 。通过这一行,代码可以很好地工作。