简体   繁体   English

上传图片以及Google App Engine

[英]Uploading images along with Google App Engine

I'm working on a Google App Engine project. 我正在开发一个Google App Engine项目。

My app is working and looking correct locally, but when I try to upload images in an image directory, they're not being displayed at appspot. 我的应用程序正在本地工作并且看起来正确,但是当我尝试在图像目录中上传图像时,它们不会在appspot上显示。

As a little troubleshoot, I put a HTML page in "/images/page2.html" and I can load that page at the appspot, but my pages don't display my images. 作为一个小故障,我在“/images/page2.html”中放置了一个HTML页面,我可以在appspot加载该页面,但我的页面不显示我的图像。 So, it's not a problem with my path. 所以,这对我的道路来说不是问题。

As another sanity check, I'm also uploading a style sheet directory with .css code in it, and that's being read properly. 作为另一个健全性检查,我也在上传一个带有.css代码的样式表目录,并且正在正确读取。

I have a suspicion that the problem lies in my app.yaml file. 我怀疑问题出在我的app.yaml文件中。

Any ideas? 有任何想法吗?

I don't want to paste all the code here, but here are some of the key lines. 我不想在这里粘贴所有代码,但这里有一些关键行。 The first two work fine. 前两个工作正常。 The third does not work: 第三个不起作用:

<link type="text/css" rel="stylesheet" href="/stylesheets/style.css" />
<a href="/images/Page2.html">Page 2</a>
<img src="/images/img.gif">

This is my app.yaml file 这是我的app.yaml文件

application: myApp
version: 1
runtime: python
api_version: 1

handlers:
- url: /stylesheets
  static_dir: stylesheets

- url: /images
  static_dir: images

- url: /.*
  script: helloworld.py

You have to configure app.yaml for static content such as images and css files 您必须为静态内容(如图像和css文件)配置app.yaml

Example: 例:

 url: /(.*\.(gif|png|jpg))
  static_files: static/\1
  upload: static/(.*\.(gif|png|jpg))

For more info check out: http://code.google.com/appengine/docs/configuringanapp.html 有关详细信息,请访问: http//code.google.com/appengine/docs/configuringanapp.html

I'll bet your problem is that you're using Windows. 我敢打赌你的问题是你正在使用Windows。

If that's the case, I believe you need a preceding slash for your static_dir value. 如果是这种情况,我相信你需要一个前面的斜线来表示你的static_dir值。

I am using the Java version of App engine, and I faced a similar issues with the server not able to serve static images. 我正在使用Java版本的App引擎,我遇到了类似的问题,服务器无法提供静态图像。

What worked ultimately was to change the AppEngine config file "appengine-web.xml" in my case to contain 最终的作用是将我的案例中的AppEngine配置文件“appengine-web.xml”更改为包含

<static-files>
<include path="**.*"/>
    <include path="/images/**.*" />
</static-files>

My images are in the /images directory and HTML and CSS are in . 我的图像位于/ images目录中,HTML和CSS位于。 directory which is at the WEB-INF level 目录在WEB-INF级别

@jamtoday The preceding slash didn't make a difference, but it did get me started figuring out what each app needs to be told what about my directory structure. @jamtoday上面的斜杠并没有什么区别,但它确实让我开始弄清楚每个应用程序需要告诉我的目录结构是什么。

So, I have nothing very conclusive to add, but I wanted to follow up, because I got it working, but I didn't explore all the issues after I got it working. 所以,我没有什么非常的结论可以添加,但我想跟进,因为我让它工作,但我没有在我开始工作之后探讨所有问题。

One change that helped was to stop working from a HwlloWorld/src/ directory and start working in the HelloWorld/ directory. 一个有帮助的变化是停止从HwlloWorld / src /目录开始工作并开始在HelloWorld /目录中工作。 It seems like the dev_appserver picked up all the dependencies, but the remote server didn't. 似乎dev_appserver拾取了所有依赖项,但远程服务器没有。 Essentially, the relative path of my local links didn't match the relative path of the links after uploading. 本质上,我本地链接的相对路径与上传后链接的相对路径不匹配。

I also realized that the dev-appserver relies on the .yaml file, as well as the appcfg script. 我还意识到dev-appserver依赖于.yaml文件以及appcfg脚本。 That is. 那是。 . .if you add a directory to your project, and then try to link to files in that directory, you need to add the directory to the .yaml file, and then restart the dev-appserver to pick up on this. 。如果你在项目中添加一个目录,然后尝试链接到该目录中的文件,你需要将该目录添加到.yaml文件中,然后重新启动dev-appserver来获取它。

So, there are probably ways to handle what I was originally trying to do if you give the .yaml file the right info, but changing to a different directory structure locally handled it for me. 因此,如果您为.yaml文件提供正确的信息,可能有办法处理我最初尝试做的事情,但更改为不同的目录结构在本地为我处理它。

<img src="/images/img.gif">

this line can't show you the image. 此行无法显示图像。 Try this one: 试试这个:

1-Create a class to handle "image request" 1 - 创建一个类来处理“图像请求”

class GetImage(webapp.RequestHandler):
      def get(self):
       self.response.headers['Content-Type'] = 'image/jpg'
       self.response.out.write(image_object)

2-In your page.html: 2 - 在你的page.html中:

<img src="/image"

3-At the main function in your code.py: 3 - 在code.py的main函数中:

application = webapp.WSGIApplication(('/image', GetImage), debug=True)

have fun 玩得开心

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

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