从GAE下载用户的数据

时间:2018-02-15 14:58:45

标签: google-app-engine google-cloud-datastore

glowscript.org是一个基于Python的GAE,用于在数据存储区中存储用户编写的脚本。如何在Python服务器和/或JavaScript客户端中编写一个按钮,让用户可以将一个或多个用户脚本下载到用户的下载目录?我在搜索中找到的所有内容似乎都是面向我从命令行下载数据存储区,这似乎与我的情况无关。

1 个答案:

答案 0 :(得分:1)

编辑:我修改了我的csv示例以下载python文件(.py)而不是

我的数据存储模型

class MetricsJob(ndb.Model):
    result = ndb.TextProperty(compressed=True) # the file body
    name = ndb.StringProperty()
    # ... other fields

我的请求处理程序

class MetricsDownload(webapp2.RequestHandler):
    def get(self, key):
        obj = ndb.Key(urlsafe=key).get()
        self.response.headers['Content-disposition'] = 'attachment; filename='+obj.name+'.py'
        self.response.headers['Content-Transfer-Encoding'] = 'Binary'
        self.response.headers['Content-Type'] = 'text/plain'
        self.response.out.write(obj.result)

HTML代码,您只需启动HTTP GET:

<a target="_blank" href="/metrics/download/ahFkZXZ-cGF5LXRvbGxvLXdlYnIRCxIEVXNlchiAgICAgODECww/"></a>

让浏览器将HTTP GET视为文件下载的关键是响应头。

对于您的用例,我想您需要设置:

self.response.headers['Content-Type'] = 'text/plain'

我不确定其他人

相关问题