我正在使用 python 在谷歌应用程序引擎上构建一个 webapp,我有以下代码允许用户上传图像,但是如果有人上传不是图像的东西,代码就会崩溃,因为我试图从数据,它当然不接受任何不是图像文件的东西。基本上我想知道是否有一种简单的方法可以防止上传不是图像文件的文件。
class RecordPage(BlogHandler):
def get(self, keyid, title):
record = individual_record_cache(keyid)
upload_url = blobstore.create_upload_url('/upload/%s' % (keyid))
photos = []
for blobRef in record.blobRefs:
photos.append(images.get_serving_url(blobRef, size=None, crop=False, secure_url=None))
global visits
user = users.get_current_user()
logout = users.create_logout_url(self.request.uri)
self.render("recordpermalink.html", user=user, logout=logout, record=record, visits=visits, photos=photos, upload_url=upload_url, keyid=keyid)
class UploadHandler(blobstore_handlers.BlobstoreUploadHandler):
def post(self, record_id):
upload_files = self.get_uploads('file')
blob_info = upload_files[0]
blobRef = blob_info.key()
record = Record.get_by_id(int(record_id))
record.blobRefs.append(blobRef)
record.put()
individual_record_cache(record_id, True)
record_cache(True)
profile_record_cache(True)
self.redirect('/record/%s/%s' % (record_id, record.title))