Python应用引擎:如何保存图像?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Python应用引擎:如何保存图像?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3826字,纯文字阅读大概需要6分钟。
内容图文
![Python应用引擎:如何保存图像?](/upload/InfoBanner/zyjiaocheng/813/db1b5b5649694553aa65bf6023834d58.jpg)
这是我从flex 4文件引用上传得到的:
self.request =
Request: POST /UPLOAD
Accept: text/*
Cache-Control: no-cache
Connection: Keep-Alive
Content-Length: 51386
Content-Type: multipart/form-data; boundary=----------ei4cH2gL6ae0ei4ae0gL6GI3KM7ei4
Host: localhost:8080
User-Agent: Shockwave Flash
------------ei4cH2gL6ae0ei4ae0gL6GI3KM7ei4
Content-Disposition: form-data; name="Filename"
36823_117825034935819_100001249682611_118718_676534_n.jpg
------------ei4cH2gL6ae0ei4ae0gL6GI3KM7ei4
Content-Disposition: form-data; name="Filedata"; filename="36823_117825034935819_100001249682611_118718_676534_n.jpg"
Content-Type: application/octet-stream
???? [AND OTHER STRANGE CHARACTERS]
我的课:
class Upload(webapp.RequestHandler):
def post(self):
content = self.request.get("Filedata")
return "done!"
现在我在Upload类中缺少什么才能将该文件保存到磁盘?
我在内容var中有一些奇怪的字符(在调试中查看).
解决方法:
An App Engine application cannot:
- write to the filesystem. Applications must use the App Engine
datastore for storing persistent data.
您需要做的是向用户呈现带有文件上载字段的表单.
提交表单后,文件将上载,Blobstore将从文件内容中创建一个blob,并返回一个blob键,可用于稍后检索和提供blob.
允许的最大对象大小为2千兆字节.
这是一个可以尝试的工作代码段:
#!/usr/bin/env python
#
import os
import urllib
from google.appengine.ext import blobstore
from google.appengine.ext import webapp
from google.appengine.ext.webapp import blobstore_handlers
from google.appengine.ext.webapp import template
from google.appengine.ext.webapp.util import run_wsgi_app
class MainHandler(webapp.RequestHandler):
def get(self):
upload_url = blobstore.create_upload_url('/upload')
self.response.out.write('<html><body>')
self.response.out.write('<form action="%s" method="POST" enctype="multipart/form-data">' % upload_url)
self.response.out.write("""Upload File: <input type="file" name="file"><br> <input type="submit"
name="submit" value="Submit"> </form></body></html>""")
class UploadHandler(blobstore_handlers.BlobstoreUploadHandler):
def post(self):
upload_files = self.get_uploads('file')
blob_info = upload_files[0]
self.redirect('/serve/%s' % blob_info.key())
class ServeHandler(blobstore_handlers.BlobstoreDownloadHandler):
def get(self, resource):
resource = str(urllib.unquote(resource))
blob_info = blobstore.BlobInfo.get(resource)
self.send_blob(blob_info)
def main():
application = webapp.WSGIApplication(
[('/', MainHandler),
('/upload', UploadHandler),
('/serve/([^/]+)?', ServeHandler),
], debug=True)
run_wsgi_app(application)
if __name__ == '__main__':
main()
EDIT1:
在您的特定情况下,您可以使用BlobProperty(限制为1MB)来存储您的请求:
class Photo(db.Model):
imageblob = db.BlobProperty()
然后调整你的webapp.RequestHandler来保存你的请求:
class Upload(webapp.RequestHandler):
def post(self):
image = self.request.get("Filedata")
photo = Photo()
photo.imageblob = db.Blob(image)
photo.put()
EDIT2:
您无需更改app.yaml,只需添加新的处理程序并将其映射到WSGI中即可.
要检索存储的照片,您应该添加另一个处理程序来为照片提供服务:
class DownloadImage(webapp.RequestHandler):
def get(self):
photo= db.get(self.request.get("photo_id"))
if photo:
self.response.headers['Content-Type'] = "image/jpeg"
self.response.out.write(photo.imageblob)
else:
self.response.out.write("Image not available")
然后映射你的新DownloadImage类:
application = webapp.WSGIApplication([
...
('/i', DownloadImage),
...
], debug=True)
您可以使用以下网址获取图片:
yourapp/i?photo_id = photo_key
根据要求,如果出于任何奇怪的原因,您真的想使用这种网址www.mysite.com/i/photo_key.jpg来提供图像,您可能想尝试这样做:
class Download(webapp.RequestHandler):
def get(self, photo_id):
photo= db.get(db.Key(photo_id))
if photo:
self.response.headers['Content-Type'] = "image/jpeg"
self.response.out.write(photo.imageblob)
else:
self.response.out.write("Image not available")
略有不同的映射:
application = webapp.WSGIApplication([
...
('/i/(\d+)\.jpg', DownloadImage),
...
], debug=True)
内容总结
以上是互联网集市为您收集整理的Python应用引擎:如何保存图像?全部内容,希望文章能够帮你解决Python应用引擎:如何保存图像?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。