2017-09-21 6 views
0

나는 아래의 코드를 사용하여 단어 문서를 생성 한 다음 cherrypy를 사용하여 웹에서이 코드를 제공합니다.우편 번호 및 서버 다중 메모리 파일

tpl.get_docx().save(iostream) 
cherrypy.response.headers['Content-Type'] = (
      'application/vnd.openxmlformats-officedocument' 
      '.wordprocessingml.document' 
      ) 
cherrypy.response.headers['Content-Disposition'] = (
      'attachment; filename={fname}.docx'.format(
       fname='SP' + kwargs['sp'] + '-'+ kwargs['WO'] + ' ' + kwargs['site'] + ' - ' + 'RPC Report' +'.docx' 
      ) 
      ) 
iostream.seek(0) 
return file_generator(iostream) 

더 많은 문서를 만든 다음 메모리에 압축하여 웹에 제공 할 계획입니다. 이것이 어떻게 구현 될 수 있었는지, 나는 zipfile 라이브러리를 사용해 보았는데, 메모리 파일을 압축하는 것이 복잡해 보입니다.

다음 예는 Google에서 내 문제를 해결할 수 있지만이를 사용하는 방법을 잘 모르겠습니다.

import zipfile 
import StringIO 

zipped_file = StringIO.StringIO() 
with zipfile.ZipFile(zipped_file, 'w') as zip: 
    for i, file in enumerate(files): 
     file.seek(0) 
     zip.writestr("{}.csv".format(i), file.read()) 

zipped_file.seek(0) 

답변

2

지속성의 시간 후, 난 당신이 대답 표시해야합니다 yeahhhh,

iostream = BytesIO() 
tpl.get_docx().save(iostream) 

iostream1 = BytesIO() 
tpl1.get_docx().save(iostream1) 

zip_output = StringIO.StringIO() 
file = zipfile.ZipFile(zip_output, "w") 
file.writestr("test.docx", iostream.getvalue()) 
file.writestr("test1.docx", iostream1.getvalue()) 
file.close() 

cherrypy.response.headers['Content-Type'] = 'application/zip' 
cherrypy.response.headers['Content-Disposition'] = 'attachment; filename="test.zip"' 

return zip_output.getvalue() 
+0

을이 작업을 가지고 ... – 576i