简体   繁体   English

上传图片-Google App Engine + Python

[英]Uploading images - Google App Engine + Python

I'm using this link as an example to uploading images: 我使用此链接作为上传图像的示例:

https://gist.github.com/jdstanhope/5079277 https://gist.github.com/jdstanhope/5079277

My HTML code: 我的HTML代码:

 <form action="/upload_image" method="post" id="form1" runat="server">
       <div class="fileButtons">
         <input type='file' id="imgInp" name="imgInput" accept="image/*"/><br><br>
         <input type='button' id='remove' value='Remove' />
        </div></form>

main.py: main.py:

class SetImage(webapp2.RequestHandler):
    def post(self):
        logging.debug("test")
        id = str(self.request.get('id'))
        image = self.request.get('imgInput')

app = webapp2.WSGIApplication([('/upload_image', SetImage),
                               ('/', MainPage)], debug=True)

But when I add an image, nothing is being done, and the log console doesn't print: 但是,当我添加图像时,什么也没做,并且日志控制台无法打印:

logging.debug("test")

The recommended way of uploading images to GAE is by using blobstore . 建议将图像上传到GAE的方法是使用blobstore

Here is a quick breakdown of the doc to help you achieve this fast: 以下是该文档的快速细分,可帮助您快速完成此任务:

Imports: 进口:

import os
import urllib
import webapp2

from google.appengine.ext import blobstore
from google.appengine.ext.webapp import blobstore_handlers

Serve the form HTML. 提供表单HTML。 this form performs the POST to GAE with the selected files data: 此表单使用所选文件数据执行POST到GAE:

class MainHandler(webapp2.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>""")

Add a handler for receiving the POST data (binary content of the file). 添加一个用于接收POST数据(文件的二进制内容)的处理程序。 the last line in this function is to redirect the response to the location from where the file could be downloaded: 此函数的最后一行是将响应重定向到可以从中下载文件的位置:

class UploadHandler(blobstore_handlers.BlobstoreUploadHandler):
    def post(self):
        # 'file' is file upload field in the form
        upload_files = self.get_uploads('file')
        blob_info = upload_files[0]
        self.redirect('/serve/%s' % blob_info.key())

Add handler to serve the image that you uploaded using the UploadHandler described above: 添加处理程序以提供您使用上述的UploadHandler上传的图像:

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)

And finally, define the routes for the app: 最后,为应用定义路由:

app = webapp2.WSGIApplication([('/', MainHandler),
                           ('/upload', UploadHandler),
                           ('/serve/([^/]+)?', ServeHandler)],
                            debug=True)

Regarding the issue of your log statement not firing: The default log level for dev_appserver.py is info , which you can override with the --dev_appserver_log_level flag. 关于日志语句未触发的问题: dev_appserver.py的默认日志级别为info ,您可以使用--dev_appserver_log_level标志进行覆盖。

Reference the new dev_appserver documentation . 请参考新的dev_appserver文档

To access uploaded files from a multipart/form-data POST request, the webapp2 documentation states that they're available in request.POST . 要从multipart / form-data POST请求访问上传的文件, webapp2文档声明它们在request.POST可用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM