从 GAE 下载用户数据



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

编辑:我修改了我的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获取作为文件下载的关键是响应标题。

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

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

我不确定其他人

最新更新