如何让Google App Engine具有从数据库下载内容的下载链接? [英] How do I let Google App Engine have a download link that downloads something from a database?

查看:129
本文介绍了如何让Google App Engine具有从数据库下载内容的下载链接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,让我们说我有一个数据库

Okay, let's say I have a database

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

我想在网页上提供一个下载链接, 。我怎么做到这一点?

And I wanted to supply a download link on the webpage that would download the content of code. How would I do this?

我正在使用python和jinja2

I am using python and jinja2

推荐答案

您将创建一个视图,返回代码的内容(假设您使用的是webapp框架):

You'd create a view that sends back the content of code (assuming that you're using the "webapp" framework):

class MainPage(webapp.RequestHandler):
    def get(self):
        content = Content.get(…)
        self.response.headers['Content-Type'] = 'application/octet-stream'
        self.response.out.write(content.code)

请注意,您可能希望将 Content-Type 设置为更具体的内容。另外,如果你想强制浏览器下载文件(而不是显示文件),你可以设置 Content-Disposition 头:标题['Content-Disposition'] ='附件; filename = some_filename.txt'

Note that you might want to set the Content-Type to something more specific. Also, if you want to force the browser to download the file (instead of possibly displaying the file), you can set the Content-Disposition header: headers['Content-Disposition'] = 'attachment; filename=some_filename.txt'.

这篇关于如何让Google App Engine具有从数据库下载内容的下载链接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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