从 GAE 下载用户数据

Download from GAE a user's data

glowscript.org 是一个基于 Python 的 GAE,它将用户编写的脚本存储在数据存储中。我如何在 Python 服务器 and/or 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 GET 视为文件下载的关键是响应 headers。

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

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

我不确定其他人