1

我的图像没有从数据存储中提供,我在图像应该在的页面上收到 404。

这是代码:

import cgi
import datetime
import urllib
import wsgiref.handlers

from google.appengine.ext import db
from google.appengine.api import images
from google.appengine.api import users
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app


class PropUpload(db.Model):
  property_name = db.StringProperty()
  property_address = db.StringProperty()
  project_type = db.StringProperty()
  building_size = db.StringProperty()
  stories = db.StringProperty()
  space_available = db.StringProperty()
  per_leased = db.StringProperty()
  prop_image = db.BlobProperty()
  date = db.DateTimeProperty(auto_now_add=True)


class MainPage(webapp.RequestHandler):
  def get(self):
    self.response.out.write("""<!DOCTYPE html>
            <html lang="en">
              <head>
      <somehtml>""")

    display2 = db.Query(PropUpload)
    display2.order("-date")
    for dis in display2:
        self.response.out.write('<div><img src="img?img_id=%s"></img>' % dis.key())
        self.response.out.write('<h4>Property Name: %s </h4><br>' % dis.property_name)
        self.response.out.write('<div style="border-bottom: 1px solid #CCC;">Address: %s </div><br>' % dis.property_address)
        self.response.out.write('<div style="border-bottom: 1px solid #CCC;">Type:  %s ft<sup>2</sup></div><br>' % dis.project_type)
        self.response.out.write('<div style="border-bottom: 1px solid #CCC;">Stories:  %s </div><br>' % dis.stories)

    self.response.out.write("<more html>")


class Image(webapp.RequestHandler):
  def get(self):
    img_id = self.request.get("img_id")
    dis = db.get(self.request.get("img_id"))
    if dis.prop_image:
      self.response.headers['Content-Type'] = "image/png"
      self.response.out.write(dis.prop_image)
    else:
      self.response.out.write("no image")


class UploadPage2(webapp.RequestHandler):
  def get(self):
    self.response.out.write("""<!DOCTYPE html>
    <form action="/upload2" enctype="multipart/form-data" method="post">
                        <div><label>Property Name:&nbsp</label></div>
                        <div><textarea name="prop-name" rows="1" columns "100"></textarea></div>
                        <br><br>
                        <div><label>Property Address:&nbsp</label></div>
                        <div><textarea name="address" rows="1" columns "100"></textarea></div>
                        <br><br>
                        <div><label>Property Type:&nbsp</label></div>
                        <div><textarea name="prop-type" rows="1" columns "100"></textarea></div>
                        <br><br>
                        <div><label>Building Size:&nbsp</label></div>
                        <div><textarea name="size" rows="1" columns "100"></textarea></div>
                        <br><br>
                        <div><label>Stories:&nbsp</label></div>
                        <div><textarea name="stories" rows="1" columns "100"></textarea></div>
                        <br><br>
                        <div><label>Space Available:&nbsp</label></div>
                        <div><textarea name="title" rows="2" columns "100"></textarea></div>
                        <br>
                        <div><label>Percent Leased:</label></div><span>
                        <div><textarea name="percent" rows="3" cols="100"></textarea></div><br>
                        <div><label>Picture:</label></div>
                        <div><input type="file" name="img"/></div>
                        <div><label></label></div><span>
                        <div><input type="submit" value="Upload" /></div>
                      </form>""")


  def post(self):
    props = PropUpload()
    property_name = self.request.get('prop-name')
    property_address = self.request.get('address')
    property_type = self.request.get('prop=type')
    building_size = self.request.get('size')
    stories = self.request.get('stories')
    space_available = self.request.get('space')
    prop_image = self.request.get('img')
    per_leased = self.request.get('percent')
    props.property_name = property_name
    props.property_address = property_address
    props.property_type = property_type
    props.building_size = building_size
    props.stories = stories
    props.space_available = space_available
    props.prop_image = db.Blob(prop_image)
    props.per_leased = per_leased
    props.put()
    self.redirect('/upload2')


application = webapp.WSGIApplication([
    ('/', MainPage),
    ('/upload2', UploadPage2),
    ], debug=True)


def main():
  run_wsgi_app(application)

if __name__ == '__main__':
  main()

一切正常,除了 404,我找不到任何搜索这个问题的东西。

感谢您的帮助,很抱歉,如果我遗漏了任何内容,我会编辑任何内容。

4

1 回答 1

2

假设您的上传工作正常,您需要为图像本身提供一个处理程序。现在您正在引用<img src="img?img_id=%s">,但img似乎无法解析处理程序中的任何内容(此外,您不需要为HTML</img>中的属性提供结束标记)。<img>您要做的是为基于图像的请求设置一个处理程序,并让响应是图像本身。您已经Image为该目的进行了设置(请注意,您可能希望将no image部件替换为实际图像,以便结果类型保持一致)。

要将处理程序映射Image到 URL,请尝试按如下方式修改处理程序:

application = webapp.WSGIApplication([
    ('/', MainPage),
    ('/img', Image),
    ('/upload2', UploadPage2),
    ], debug=True)

现在,这会将请求路由到/img处理Image程序(您可能还希望将src标签更改为<img src="/img?img_id=%s">,然后它应该定位并返回您的图像。

于 2012-12-18T21:49:18.790 回答