我正在尝试显示上传的 blob 图像。它似乎可以存储,但调用时不会显示在 html 模板上。甚至似乎出现在日志记录中。在查看其他示例时,我看到的唯一真正区别是您必须遍历列表,但实际上只想拉一张图片。非常感谢您提供的任何建议。-谢谢
处理程序:
class PictureHandler(BaseHandler2):
def get(self, **kwargs):
user_session = self.user
user_session_object = self.auth.store.get_session(self.request)
user_info = models.User.get_by_id(long( self.user_id ))
user_info_object = self.auth.store.user_model.get_by_auth_token(
user_session['user_id'], user_session['token'])
user = self.session.get('user')
uploads = db.GqlQuery("SELECT * FROM UserPictureUpload WHERE user =:1 ORDER BY created DESC", user_info.username).get()
upload_url = blobstore.create_upload_url('/upload')
params = {
'upload_url': upload_url,
'user': user_info.username,
'uploads': uploads
}
return self.render_template('picture.html', **params)
class UploadHandler(blobstore_handlers.BlobstoreUploadHandler, BaseHandler2):
def post(self):
user_session = self.user
user_session_object = self.auth.store.get_session(self.request)
user_info = models.User.get_by_id(long( self.user_id ))
user_info_object = self.auth.store.user_model.get_by_auth_token(
user_session['user_id'], user_session['token'])
user = self.session.get('user')
title = self.request.get('title')
pic = self.request.get("picfile")
if pic:
picture = db.Blob(pic)
upload = UserPictureUpload(title = title, picture=picture, user=user_info.username)
upload.put()
self.redirect('/settings/picture')
class ViewHandler(blobstore_handlers.BlobstoreDownloadHandler, BaseHandler2):
def get(self):
user_session = self.user
user_session_object = self.auth.store.get_session(self.request)
user_info = models.User.get_by_id(long( self.user_id ))
user_info_object = self.auth.store.user_model.get_by_auth_token(
user_session['user_id'], user_session['token'])
user = self.session.get('user')
upload_key_str = self.request.params.get('key')
if upload_key_str:
upload = db.get(upload_key_str)
if upload:
logging.info('** Upload Found** -- %s' % upload.picture)
self.send_blob(upload.picture)
if not upload_key_str:
self.error(404)
return
路由:
RedirectRoute('/settings/picture', handlers.PictureHandler, name='picture', strict_slash=True),
RedirectRoute('/upload', handlers.UploadHandler, name='upload', strict_slash=True),
RedirectRoute('/view', handlers.ViewHandler, name='view', strict_slash=True)
html:
<form action="{{ upload_url }}" method="post" enctype="multipart/form-data">
<label for="title">Title:</label>
<input type="text" id="title" name="title" /><br />
<label for="upload">File:</label>
<input type="file" id="upload" name="picfile" /><br />
<input type="submit" value="Upload Picture" />
</form>
<br />
<img src='/view?key={{uploads.key()}}' alt='no image'/></img>
应用引擎日志:
信息 2013-07-30 16:11:20,946 handlers.py:875] ** 上传找到** -- 内容类型:图像/jpeg
内容长度:775702
内容-MD5:NWE0NGM3YmE1YmJlNGVjODY3MjMzZDY3ZTQ4MDY4NDg=
内容处置:表单数据;名称="图片文件"; 文件名="水母.jpg"
X-AppEngine-Upload-Creation: 2013-07-29 15:41:00.481000
信息 2013-07-30 11:11:21,005 server.py:584] 默认值:“GET /view?key=ag5kZXZ-c2FuZGVuZ2luZXIeCxIRVXNlclBpY3R1cmVVcGxvYWQYgICAgJjxtgsM HTTP/1.1”200 -