0

谷歌关于使用 GAE 动态提供图像的说明在这里:https ://developers.google.com/appengine/articles/python/serving_dynamic_images 唯一的问题是这种方法不使用 AJAX,我不认为它是和我期望的一样高效。

我正在尝试将 AJAX 与 GAE Python 2.7 一起使用来动态提供多个图像,但我现在专注于提供一个。

Python 服务器端代码:

class GetImage(webapp2.RequestHandler):
    def get(self):
        problem = Problem.all()[0]
        if(problem and problem.prompt_image):
            self.response.headers['Content-Type'] = "image/png"
            self.response.out.write(problem.prompt_image)

客户端 AJAX 代码:

$.ajax({
    url: "/img",
    type: "POST",
    dataType: "html",
    success: function(msg){
        $("#right").append("<img>"+msg+"</img>");
    }
});

我面临的问题是我不知道如何通过 AJAX 将 blobstore 内容提供给 DOM。谁能指出我正确的方向?

谢谢!

编辑:voscausa 的解决方案似乎是在正确的轨道上。但是,它需要一个 blob_key。我的模型如下:

class Problem(db.Model):
    prompt_image = db.BlobProperty()

我似乎无法弄清楚如何从 db.BlobProperty 获取 Blob 密钥。这可能吗?

4

2 回答 2

1

要使用 GET 从 blobstore 提供图像:

http://{{ your_appid }}.appspot.com/imgserve/{{ blob_key }}

从 blobstore 提供图像的代码:

class ImgServe(blobstore_handlers.BlobstoreDownloadHandler):

    def get(self, blob_key):                                       

    blob_info = blobstore.BlobInfo.get(blob_key)     
    self.send_blob(blob_info, save_as=True)


app = webapp2.WSGIApplication(
                          [
                           ('/imgserve/([^/]+)?', ImgServe),
                          ], debug=True)   
于 2012-10-29T01:45:20.970 回答
1

这个问题让我非常头疼。感谢所有人,尤其是 voscausa,他们让我走上了获得解决方案的正确道路。那里有很多旧材料,这是我使用的解决方案: https ://developers.google.com/appengine/docs/python/blobstore/overview#Uploading_a_Blob (请参阅完整的示例应用程序)

import os
import urllib
import webapp2

from google.appengine.ext import blobstore
from google.appengine.ext.webapp import blobstore_handlers

class MainHandler(webapp2.RequestHandler):
  def get(self):
    upload_url = blobstore.create_upload_url('/upload')
    self.response.out.write('<html><body>')
    self.response.out.write('<form action="%s" method="POST" enctype="multipart/form-data">' % upload_url)
    self.response.out.write("""Upload File: <input type="file" name="file"><br> <input type="submit"
        name="submit" value="Submit"> </form></body></html>""")

class UploadHandler(blobstore_handlers.BlobstoreUploadHandler):
  def post(self):
    upload_files = self.get_uploads('file')  # 'file' is file upload field in the form
    blob_info = upload_files[0]
    self.redirect('/serve/%s' % blob_info.key())

class ServeHandler(blobstore_handlers.BlobstoreDownloadHandler):
  def get(self, resource):
    resource = str(urllib.unquote(resource))
    blob_info = blobstore.BlobInfo.get(resource)
    self.send_blob(blob_info)

app = webapp2.WSGIApplication([('/', MainHandler),
                               ('/upload', UploadHandler),
                               ('/serve/([^/]+)?', ServeHandler)],
                              debug=True)
于 2012-11-02T04:07:19.567 回答