2012-10-29 37 views
0

谷歌關於動態提供GAE圖像的說明在這裏:https://developers.google.com/appengine/articles/python/serving_dynamic_images 唯一的問題是,這種方法不使用AJAX,我不認爲它是如我所期望的那樣高效。基於AJAX的動態圖像服務 - 谷歌App Engine Python 2.7

我正在嘗試使用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服務Blob存儲內容DOM。任何人都可以將我指向正確的方向嗎?

謝謝!

編輯:voscausa的解決方案似乎是在正確的軌道上。但是,它需要一個blob_key。我的模型如下:

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

我似乎無法弄清楚如何從db.BlobProperty獲取Blob密鑰。這可能嗎?

+0

你需要做一個POST? –

+0

您是否需要在瀏覽器中查看它,或者您需要下載它。 –

+0

@Peter在整個系統中,用戶會按下一個按鈕,這會觸發一個AJAX事件。由於那時候,頁面已經被加載了,我覺得POST是合適的。 –

回答

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) 
1

爲了從Blob存儲服務的圖像與GET:

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

代碼從Blob存儲服務形象:

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) 
+0

voscausa,感謝您的快速回復。我對你的回答有點困惑。根據我的理解,當用戶想要獲取圖像時,我可以讓我的JQuery JS進行AJAX調用,然後獲取返回的數據並將其顯示在頁面上。看來你的解決方案需要用戶導航到一個URL。 –

+0

用你的話來解釋一下:一個AJAX GET就像導航到一個url。網頁中的圖片網址是圖片GET =轉到顯示圖片的網址。和some_text:在網頁中顯示圖像。 – voscausa

+0

用你的話來解釋它:一個AJAX GET就像導航到一個url,它返回的圖像。網頁中的圖片網址是圖片GET =轉到網址,它顯示/返回圖片。和some_text:在網頁上顯示圖片 – voscausa