Skip to main content
 首页 » 编程设计

python之Google App Engine 上传图片 Blobstore

2024年10月25日2352php

我想让用户上传图片并为此上传制作演示,并阅读 GAE Blobstore API。

我的问题是在提交图像文件时出现错误 AttributeError: 'UploadHandlerDemo' object has no attribute 'get' but Blob class is already created in datastore.我该如何处理这个问题?

这是我的代码片段:

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: 'UploadHandlerDemo' 对象没有属性 'get'

我尝试了很多次并查看了 stackoverflow,但无法找到解决方案。

请您参考如下方法:

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

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