Rails - How to send an image from a controller

前端 未结 2 1520
眼角桃花
眼角桃花 2020-12-24 08:06

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_         


        
相关标签:
2条回答
  • 2020-12-24 08:43

    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:

    send_data open("#{Rails.root}/path/to/file.gif", "rb") { |f| f.read } .......
    

    The rb sets the file to open and binary modes.

    0 讨论(0)
  • 2020-12-24 08:47

    Wouldn't be better to use send_file instead?

    send_file Rails.root.join("public", "file.gif"), type: "image/gif", disposition: "inline"
    
    0 讨论(0)
提交回复
热议问题