谷歌应用程序引擎下载包含文件的文件 [英] google app engine download a file containing files

查看:145
本文介绍了谷歌应用程序引擎下载包含文件的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,我有:

$ p $ class Content(db.Model):
code = db.TextProperty( )

存储在数据库中的代码有3种不同的值。我将如何创建一个zip文件,将三个代码值存储在三个可以下载的独立文件中?



基于eric.f的回答:
我重写了他的代码,使其能够完成我想要的功能:

  contents = db.GqlQuery(SELECT * FROM Content ORDER BY created DESC)
output = StringIO.StringIO()
with zipfile.ZipFile输出'w')作为myzip:
表示内容中的内容:
如果content.code:
code = content.code
else:
code = content。 code2
myzip.writestr('udacity_code'+`content.key().id()`,code)
self.response.headers [Content-Type] =application / zip
self.response.headers ['Content-Disposition'] =attachment; filename = test.zip
self.response.out.write(output.getvalue())

我得到了一个错误...

  self.response.out.write(output.getvalue(),utf-8)
文件/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7 /StringIO.py,lin e 270,in getvalue
UnicodeDecodeError:'ascii'编解码器无法解码位置10中的字节0xb4:序号不在范围内(128)


解决方案

  import zipfile 
import StringIO

output = StringIO。 StringIO()

带zipfile.ZipFile(输出,'w')as myzip:
myzip.writestr('file1.txt','aaaaaaaaa')
myzip.writestr ('file2.txt','bbbbbbbbb')
myzip.writestr('file3.txt','ccccccccc')

然后作出回应,将 output.getvalue()设为内容,并设置标题如下:

 内容类型:application / zip 
内容处置:附件; filename = test.zip


Okay, I have:

class Content(db.Model):
    code=db.TextProperty()

And there are 3 different values of code stored in the database. How would I create a zip file that stores the three values of code in 3 separate files that would be downloaded?

Based on eric.f's answer: I rewrote his code to make it to do what I wanted:

    contents = db.GqlQuery("SELECT * FROM Content ORDER BY created DESC")
    output = StringIO.StringIO()
    with zipfile.ZipFile(output, 'w') as myzip:
        for content in contents:
            if content.code:
                code=content.code
            else:
                code=content.code2
            myzip.writestr('udacity_code'+`content.key().id()`, code)
    self.response.headers["Content-Type"] = "application/zip"
    self.response.headers['Content-Disposition'] = "attachment; filename=test.zip"
    self.response.out.write(output.getvalue())

I got an error though...

self.response.out.write(output.getvalue(), "utf-8")
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/StringIO.py", line 270, in getvalue
UnicodeDecodeError: 'ascii' codec can't decode byte 0xb4 in position 10: ordinal not in range(128)

解决方案

import zipfile
import StringIO

output = StringIO.StringIO()

with zipfile.ZipFile(output, 'w') as myzip:
    myzip.writestr('file1.txt', 'aaaaaaaaa')
    myzip.writestr('file2.txt', 'bbbbbbbbb')
    myzip.writestr('file3.txt', 'ccccccccc')

then make your response, set output.getvalue() as the content, and set headers as below:

Content-type: application/zip
Content-disposition: attachment; filename=test.zip

这篇关于谷歌应用程序引擎下载包含文件的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆