【问题标题】:Configuring MIME type配置 MIME 类型
【发布时间】:2011-04-06 11:12:25
【问题描述】:

您好,我想配置我的 mime 类型:

KML 文件的 MIME 类型是

* application/vnd.google-earth.kml+xml

如何使用谷歌应用引擎做到这一点?我在一个看起来像这样的模板上生成 KML:

<?xml version="1.0" encoding="UTF-8"?><kml xmlns="http://www.opengis.net/kml/2.2" xmlns:gx="http://www.google.com/kml/ext/2.2" xmlns:kml="http://www.opengis.net/kml/2.2" xmlns:atom="http://www.w3.org/2005/Atom">
<Document>{% for article in articles %}{% if article.geopt %}
<Placemark><name></name>
<description>
<![CDATA[{% if article.kmluri2view %}<img src="http://{{host}}/images/{{ article.kmluri2view.key.id }}.jpg">{% endif %}<a href="http://{{host}}/{{article.key.id}}"> {{ article.title }} </a><br/>{{article.text}}]]></description><Point><coordinates>{{article.geopt.lon|floatformat:2}},{{article.geopt.lat|floatformat:2}}</coordinates></Point>
</Placemark>{% endif %}{% endfor %}
</Document>
</kml>

更新了我尝试设置 MIME 类型的代码,如下所示。如何验证它是否有效?

class KMLHandler(webapp.RequestHandler):
     def get(self):            
        start=datetime.datetime.now()-timedelta(days=10)#vary  
        host = os.environ.get("HTTP_HOST", os.environ["SERVER_NAME"])       
        logging.debug('host '+host)                
        count = int(self.request.get('count')) if not self.request.get('count')=='' else 1000

        from google.appengine.api import memcache
        memcache.flush_all()
        memcache_key = "ads"
        data = memcache.get(memcache_key)
        if data is None:
          a= Ad.all().filter("modified >", start).filter("url IN", ['www.koolbusiness.com']).filter("published =", True).order("-modified").fetch(count)
          memcache.set("ads", a)  
        else:
          a = data
        dispatch='templates/kml.html'
        template_values = {'a': a , 'request':self.request, 'host':host}
        path = os.path.join(os.path.dirname(__file__), dispatch)
        self.response.headers['Content-Type'] = 'application/vnd.google-earth.kml+xml'
        self.response.out.write(template.render(path, template_values))

【问题讨论】:

  • 你使用的是哪个框架?
  • 我使用纯 webapp 运行它,只是将带有实体的列表分派到模板。如果您建议一个框架,我们非常欢迎您这样做。

标签: google-app-engine kml webapp2


【解决方案1】:

只需将响应中的Content-Type 标头设置为您想要的mimetype。例如,如果你使用 webapp,你可以这样做:

self.response.headers['Content-Type'] = 'application/vnd.google-earth.kml+xml'

【讨论】:

  • 好的,谢谢,我试试上面粘贴的,我会检查它是如何工作的
猜你喜欢
  • 2011-09-08
  • 2010-09-13
  • 1970-01-01
  • 1970-01-01
  • 2020-12-03
  • 1970-01-01
  • 2015-06-16
  • 2011-07-04
  • 1970-01-01
相关资源
最近更新 更多