Rails - 如何从控制器发送图像
在我的 Rails 应用程序中,我需要传回图像。
我的路线中有一个 1x1.gif 跟踪像素,如下所示:
match "/_ctrack.gif" => "email_tracking_pixels#my_method"
在控制器中:
def my_method
send_data open('https://www.xxxxxx.com/images/1x1_transparent.gif') {|f| f.read }, :filename => '1x1_transparent.gif', :type => 'image/gif'
end
问题是由于某种原因有时会超时。出现以下错误:
2011-03-07T20:08:36-08:00 heroku[router]: Error H12 (Request timeout) -> GET www.xxxxxxx.com/images/1x1_transparent.gif dyno=web.1 queue=0 wait=0ms service=0ms bytes=0
2011-03-07T20:08:36-08:00 app[web.1]:
2011-03-07T20:08:36-08:00 app[web.1]: OpenURI::HTTPError (503 Service Unavailable):
2011-03-07T20:08:36-08:00 app[web.1]: app/controllers/email_tracking_pixels_controller.rb:19:in `my_method'
2011-03-07T20:08:36-08:00 app[web.1]: lib/rack/www.rb:7:in `call'
关于如何传递本地存储的图像,而不是必须使用 open 并进行网络调用回我自己的服务器,有什么想法吗?
谢谢
in my rails app, I need to pass back a image.
I have a 1x1.gif tracking pixel in my route as follows:
match "/_ctrack.gif" => "email_tracking_pixels#my_method"
In the controller:
def my_method
send_data open('https://www.xxxxxx.com/images/1x1_transparent.gif') {|f| f.read }, :filename => '1x1_transparent.gif', :type => 'image/gif'
end
The problem is that for some reason sometimes this times outs. with the following error:
2011-03-07T20:08:36-08:00 heroku[router]: Error H12 (Request timeout) -> GET www.xxxxxxx.com/images/1x1_transparent.gif dyno=web.1 queue=0 wait=0ms service=0ms bytes=0
2011-03-07T20:08:36-08:00 app[web.1]:
2011-03-07T20:08:36-08:00 app[web.1]: OpenURI::HTTPError (503 Service Unavailable):
2011-03-07T20:08:36-08:00 app[web.1]: app/controllers/email_tracking_pixels_controller.rb:19:in `my_method'
2011-03-07T20:08:36-08:00 app[web.1]: lib/rack/www.rb:7:in `call'
Any ideas on how I can pass this image that's stored locally, versus having to use open and make a web call back to my own server?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是否有原因导致您无法将文件保存到
public/_ctrack.gif
、删除路由并让底层 Web 服务器提供图像?如果您需要处理磁盘中的图像,只需使用
open
本地文件名:rb
将文件设置为打开
和二进制
模式。Is there a reason that you cannot save the file to
public/_ctrack.gif
, remove the route, and let the underlying web server serve the image?If you need to process the image from disk, just use
open
on the local filename:The
rb
sets the file toopen
andbinary
modes.使用 send_file 不是更好吗?
Wouldn't be better to use send_file instead?