Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/google-app-engine/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python应用程序引擎:如何保存图像?_Python_Google App Engine_Upload - Fatal编程技术网

Python应用程序引擎:如何保存图像?

Python应用程序引擎:如何保存图像?,python,google-app-engine,upload,Python,Google App Engine,Upload,这是我从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

这是我从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类中缺少了什么? 我在内容变量中有一些奇怪的字符(在调试中查看)

应用程序引擎应用程序无法:

  • 写入文件系统。应用程序必须使用应用程序引擎 用于存储持久数据的数据存储
您需要做的是向用户呈现一个带有文件上载字段的表单。
提交表单时,将上载文件,并根据文件内容创建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()
然后调整您的
webapp.RequestHandler
以保存您的请求:

class Photo(db.Model):
 imageblob = db.BlobProperty()
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)
您将能够使用如下url获取图像:

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)

可能重复我想将文件保存到磁盘而不是blob。sorryAppEngine不支持写入文件系统。你运气不好,托蒂。您可以在应用程序部署期间上载静态文件,但这绝对是唯一的时间。检查文档:谢谢!(:但是我的应用程序是用flex制作的,我使用fileReference来上传图像1乘1。file reference发送一个bytearray对象。我只需要代码就可以将bytearray对象(即实际图像)保存到数据存储。再次感谢;)再次感谢!:)你知道如何设置我的app.yaml和ServeHandler来获取我的文件,比如“www.mysite.com/i/photo_key.jpg”?(ho visto k sei italiano xD)-不管怎样,它不能像预期的那样工作。我的url是:”,我只想要“agt0b3b3jszhiqcxijsw1hz2vcbg9igiudda”部分,那就是照片。|键|和“/i/(\d+)\.jpg'不起作用..让它发挥作用吧!更改:r'/i/(.*)\.jpg';然后:db.get(self.request.get(photo\u id))->db.get(db.Key(photo\u id))谢谢!!!