【问题标题】:My app cannot load some images from the server我的应用无法从服务器加载一些图像
【发布时间】:2011-09-26 06:16:46
【问题描述】:

我在本地 WEBrick 服务器上使用 Ruby on Rails。

我正在从 url 生成一些网站图像,将它们写入我的本地文件系统并显示它们。我用它们的 url 命名图像,但将所有 \ 替换为 -。有些图像似乎没有加载,因为它在我的文件系统上找不到图像,并且我得到了损坏的图像图标。但是,当我检查我的文件系统时,我发现所有图像都在那里。

这是我在日志中遇到的错误:

Started GET "/images/image_site/http:--www.urbandictionary.com-define.php?term=slim%20shady.jpg?1309465379" for 127.0.0.1 at Thu Jun 30 13:23:06 -0700 2011

ActionController::RoutingError (No route matches "/images/image_site/http:--www.urbandictionary.com-define.php"):

这是我的html代码:

<img alt="Http:--www.urbandictionary.com-define.php?term=slim%20shady" class="site_image" src="/images/image_site/http:--www.urbandictionary.com-define.php?term=slim%20shady.jpg?1309465379">

发生了什么,我该如何解决?如果我需要提供更多信息,请告诉我。

【问题讨论】:

    标签: ruby-on-rails ruby debugging file error-handling


    【解决方案1】:

    您似乎没有在 src 属性中正确编码您的图像名称。我猜你有一个名称如下的文件:

    http:--www.urbandictionary.com-define.php?term=slim%20shady.jpg
    

    但是当你有这个时:

    src="/images/image_site/http:--www.urbandictionary.com-define.php?term=slim%20shady.jpg?1309465379"
    

    文件名如下:

    /images/image_site/http:--www.urbandictionary.com-define.php
    

    因为第一个 ? 之后的所有内容都被认为是查询字符串的一部分。

    用连字符代替斜线是不够的,你仍然会留下各种各样的漏洞,造成混乱和邪恶的意图。相反,您应该完全生成图像文件名,例如 id.jpg 其中id 是图像在数据库中的 ID。然后,将原始文件名存储在您的数据库中,并仅向人们显示该文件名(正确编码!),不要在本地文件系统中使用它。

    快速解决方法是正确地对您的 src 属性进行 URL 编码。但是你真的应该修正你处理图像的方式,否则你会给自己带来各种各样的麻烦。

    【讨论】:

    • 我无法找出“快速修复”。 &lt;img alt="Http:--www.last.fm-music-eminem-_-the+real+slim+shady" class="site_image" src="/images/image_site/http:--www.last.fm-music-Eminem-_-The+Real+Slim+Shady.jpg?1309473638"&gt; 呢?网址中没有查询字符串。 Started GET "/images/image_site/http:--www.last.fm-music-Eminem-_-The+Real+Slim+Shady.jpg?1309473638" for 127.0.0.1 at Thu Jun 30 15:45:40 -0700 2011ActionController::RoutingError (No route matches "/images/image_site/http:--www.last.fm-music-Eminem-_-The+Real+Slim+Shady.jpg"):
    • 我切换到转义网址,但现在找不到任何网址。这是一个例子:Started GET "/images/image_site/http%3A%2F%2Fmusic.aol.com%2Falbum%2Feminem%2Fthe-slim-shady-lp%2F346579.jpg?1309475967" for 127.0.0.1 at Thu Jun 30 16:20:00 -0700 2011 ActionController::RoutingError (No route matches "/images/image_site/http%3A%2F%2Fmusic.aol.com%2Falbum%2Feminem%2Fthe-slim-shady-lp%2F346579.jpg"): 再一次,文件在那里......
    • @Justin:URL 中的+ 被翻译成to a space in a URL。您为图像使用的路线是什么?
    • 对不起,路由是什么意思(除了将传入的请求定向到控制器)?我发布的第二条路径中没有+...
    • @Justin:%3A 是冒号,%2F 是斜杠。看起来您正在使用斜杠对名称进行编码,而不是您将“斜杠连字符”应用到的名称进行编码。
    【解决方案2】:

    您需要将图像放在public/ 文件夹中,然后您可以从那里访问它们。默认情况下,Rails 将主要从 public/ 提供静态资源。

    【讨论】:

    • 我的图片在我的/public 文件夹中
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-13
    • 1970-01-01
    相关资源
    最近更新 更多