gpt4 book ai didi

Python 应用引擎 : how to save a image?

转载 作者:太空狗 更新时间:2023-10-30 02:35:12 26 4
gpt4 key购买 nike

这是我从 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):

class Upload(webapp.RequestHandler):
def post(self):
content = self.request.get("Filedata")
return "done!"

现在为了将该文件保存到磁盘,我在上传类中缺少什么?我在内容 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 GB。

这是一个您可以按原样尝试的工作片段:

#!/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()

编辑 1:
在您的特定情况下,您可以使用 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()

编辑 2:
您不需要更改您的 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

根据要求,如果出于任何奇怪的原因你真的想使用这种 url 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 应用引擎 : how to save a image?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4054471/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com