How to download file with send_file?

user3339562 picture user3339562 · Feb 25, 2014 · Viewed 22.9k times · Source

Can someone enlighten me how can I download file with send_file?

I have a file image.jpg inside app/assets/images. I've tried this in my controller:

def download
    send_file ("#{Rails.root}/public/images/image.jpg")
end

def download
    send_file ("#{Rails.root}/assets/images/image.jpg")
end

def download
    send_file ("#{Rails.root}/images/image.jpg")
end

def download
    send_file ("/public/images/image.jpg")
end

def download
    send_file ("/assets/public/images/image.jpg")
end

def download
    send_file ("/assets/images/image.jpg")
end

For each path it says:

ActionController::MissingFile in HomeController#download
Cannot read file 'some_path'

What could be a problem here? Thanks!

Answer

Coenwulf picture Coenwulf · Feb 25, 2014

Try:

IMAGES_PATH = File.join(Rails.root, "public", "images")

def download
  send_file(File.join(IMAGES_PATH, "image.jpg"))
end