0

I am not using Rails, so the send_data function will not work, unfortunately.

image = User.select("image, image_mime").where("username = '#{params[:name]}'").first
  send_data( image.image,
             :type => image.image_mime,
             :disposition => 'inline')
desbest
  • 4,746
  • 11
  • 51
  • 84

1 Answers1

2

Check out the source of send_file method:

def send_file(filename, content_type = nil, content_disposition = nil)
  content_type ||= Rack::Mime.mime_type(::File.extname(filename))
  content_disposition ||= File.basename(filename)

  response.body = ::File.open(filename, 'rb')
  response['Content-Length'] = ::File.size(filename).to_s
  response['Content-Type'] = content_type
  response['Content-Disposition'] = content_disposition
  response.status = 200

  throw(:respond, response)
end

You can try doing the same, only set the body to image.image instead of reading it from a file.

Mladen Jablanović
  • 43,461
  • 10
  • 90
  • 113
  • I tried `@user = User[view]; send_file("@user.avatarcontent")` and it's saying `Invalid argument - �PNG ` it looks like it only accepts file name locations and not strings of blobs. – desbest Sep 30 '12 at 19:31
  • You are sending the content, not the file, so you can't use `send_file` method as it is. You should change it to suit your needs instead. – Mladen Jablanović Oct 01 '12 at 05:40
  • So I'm supposed to use my brain for 10 seconds so I can make some Ruby code that displays a file with a particular encoding? That's exactly what I was trying to do! https://gist.github.com/5b3842bf970208d6bc49 – desbest Oct 07 '12 at 17:59