Google App Engine上传图片Blobstore

时间:2014-12-09 14:35:48

标签: python google-app-engine upload blobstore

我想让上传图片的用户为这个上传做一个演示,并阅读GAE Blobstore API。

我的问题是在提交图片文件时,我收到错误AttributeError:' UploadHandlerDemo'对象没有属性' get'但Blob类已在数据存储区中创建。我该如何处理这个问题?

这是我的代码段:

class FileUploadDemo(webapp2.RequestHandler):
    def get(self):
        from google.appengine.ext import blobstore
        upload_url = blobstore.create_upload_url('/admin/upload')
        logging.info(upload_url)
        blob = blobstore.BlobInfo.all().get()
        logging.info(blob.filename)
        content = {"upload_url":upload_url}
        HelperGlobal.render_template(self, 'blobstoreDemo.html', content)

这是我的UploadHandler:

from google.appengine.ext.webapp import blobstore_handlers
class UploadHandlerDemo(blobstore_handlers.BlobstoreUploadHandler):
    def post(self):
        upload_files = self.get.uploads('file')
        blob_info = upload_files[0]   
        logging.info(blob_info) 
        self.redirect('/admin/serve/%s' % blob_info.key())

在数据存储路径中,我有 BlobInfo BlobUplaodSessions ,看起来很好。

在html部分,我将GAE Blobstore示例转换为单个HTML,在JINJA的帮助下使用upload_url数据呈现。

这里是我简单演示的HTML代码:

<html>
<body>
<form action="{{upload_url}}" method="POST" enctype="multipart/form-data">
Upload File: <input type="file" name="file" id="UniqueFile">
             <br> 
             <input type="submit" name="submit" value="Submit">
</form>
</body>
</html>

当我按下提交按钮时,它创建具有相应图像的BlobInfo实体,但服务器获取代码500的错误,并说:

AttributeError:&#39; UploadHandlerDemo&#39;对象没有属性&#39; get&#39;

我有很多尝试并看起来stackoverflow但无法得到解决方案。

3 个答案:

答案 0 :(得分:1)

我认为:upload_files = self.get.uploads('file')

应该是: upload_files = self.get_uploads('file')

答案 1 :(得分:1)

你必须替换这个

upload_files = self.get.uploads('file')

upload_files = get_uploads('file')

答案 2 :(得分:0)

AttributeError:'UploadHandlerDemo'对象没有属性'get'

这可能是指这一行:

upload_files = self.get.uploads('file')

请向我们展示来自UploadHandlerDemo的其余代码。

您是否设置了“获取”属性?我不确定你要用self.get.uploads('file')做什么。你在自己中调用get对象(UploadHandlerDemo)? get对象需要拥有上传(文件)方法。