我允许一次上传 10 张图片(直接上传到 blob 商店)。image_i 与表单中的 text_i 一起使用。
并非所有内容都需要图像,因此当用户提交表单时,image_i 通常没有选定的图像文件。
我预计只会为图像上传 blob(因为这就是在开发服务器上发生的情况)。
但是,当我上线时,我在 blob 存储中看到了与内容长度为 0 的图像相对应的大量空 blob:
斑点列表>(应用/八位字节流)0字节,2012-08-14十七点11分54秒AMIfv94Y7ie8cE84WZfEWGZw-NKJPE4iUaYn25zN1J-uftmoCESQoQFBimdiZr19suNBg_kUFW0P2frxIadmpd6LzPuFuA6o9UVq5UEqSvioXaBsDQeMwdJiH5gvh-8TtlZz8Rr1fRHHZd47zsiZpDiF1XsKa6A4aKTDvCnGEdMpW8lV6nlcFfU斑点与此内容类型不能被显示。
我该如何预防这些?这是我的代码:
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):
,因为在开发服务器上它是一个空字符串,但在应用程序引擎上它不是。