我允许一次上传10张图片(直接到blob商店)。 image_i与表单中的text_i一起使用。
并非所有内容都需要图像,因此当用户提交表单时,image_i通常没有选定的图像文件。
我预计只会为图像上传blob(因为这是开发服务器上发生的事情)。
然而,当我上线时,我看到blob商店中的大量空白blob对应于内容长度为0的图像:
Blob List> (application / octet-stream)0 Bytes,2012-08-14 17:11:54 AMIfv94Y7ie8cE84WZfEWGZw-NKJPE4iUaYn25zN1J-uftmoCESQoQFBimdiZr19suNBg_kUFW0P2frxIadmpd6LzPuFuA6o9UVq5UEqSvioXaBsDQeMwdJiH5gvh-8TtlZz8Rr1fRHHZd47zsiZpDiF1XsKa6A4aKTDvCnGEdMpW8lV6nlcFfU 无法显示具有此内容类型的Blob。
我该如何预防这些?这是我的代码:
class AdminHandler(webapp2.RequestHandler):
#displays a form that includes content1, img1, content2, image2..
def get(self):
upload_url = blobstore.create_upload_url(self.uri_for('image-upload-handler'))
template_values = {'page_name': 'admin', 'upload_url': upload_url}
template = jinja_environment.get_template('admin.html')
self.response.out.write(template.render(template_values))
class BlogPost(ndb.Model):
date = ndb.DateProperty(required=True)
title = ndb.StringProperty(required=True)
blob_keys = ndb.BlobKeyProperty(repeated=True)
content = ndb.TextProperty(repeated=True)
content_order = ndb.StringProperty(repeated=True)
@classmethod
def get_blog_posts(cls):
return cls.query().order(-cls.date)
class ImageUploadHandler(blobstore_handlers.BlobstoreUploadHandler):
def post(self):
password = self.request.get('password', None)
if str(password).lower() == 'cyclist':
day = self.request.get('day', None)
month = self.request.get('month', None)
year = self.request.get('year', None)
day, month, year = map(int, (day, month, year))
try:
date = datetime(year, month, day)
#date = datetime.today()
except ValueError:
date = datetime.today()
title = self.request.get('title', None)
blob_keys = [i.key() for i in self.get_uploads()]
#assert(blob_keys)
content = []
content_order = []
for i in range(1, 11):
content_i = self.request.get("content{0}".format(i), None)
if content_i:
content_i = cgi.escape(content_i)
content_i = "<br />".join(content_i.replace("\r\r", "\r").splitlines())
content.append(content_i)
content_order.append("content_next")
img_i = self.request.get("img{0}".format(i), None)
if img_i and not "Content-Length: 0" in str(img_i):
content_order.append("image_next")
#assert(content)
blog_post = BlogPost()
blog_post.title = title
blog_post.blob_keys = blob_keys
blog_post.content = content
blog_post.date = date
blog_post.content_order = content_order
blog_post.put()
ok = True
success = "success"
else:
ok = False
success = "wrong password"
<form action="{{upload_url}}" enctype="multipart/form-data" method="post">
Date day e.g. 31: <input id='day' type="text" name="day" /><br />
Date month e.g. 12: <input id='month' type="text" name="month" /><br />
Date year e.g. 2012: <input id='year' type="text" name="year" /><br />
Title: <input id="title" type="text" name="title" /><br />
Content1: <br />
<textarea id="styled2" name="content1"></textarea>
<br />
Image1:<input type="file" name="img1"/><br />
...<br />
{% for i in range(2, 11) %}
Content{{i}}: <br />
<textarea id="styled" name="content{{i}}"></textarea>
<br />
Image{{i}}:<input type="file" name="img{{i}}"/><br />
...<br />
{% endfor %}
password: <input id="password" type="text" name="password" /><br />
<input type="submit" value="Submit" /><br />
<div id="error-box"></div>
</form>
注意我上线时必须添加and not "Content-Length: 0" in str(img_i):
,因为在开发服务器上它是一个空字符串,但在app引擎上它不是。
答案 0 :(得分:2)
这是一个known issue,我建议你添加一颗星。
在修复之前,几乎可以选择在客户端捕获空上载并且不提交它们,或者在上传处理程序中删除blobstore中长度为零的blob。