How to send file to user with carrierwave?

2019-03-26 16:54发布

问题:

Here's my old code to sends a file to the browser:

def show
  send_file File.join(Rails.root, 'tmp', 'price.xls')
end

But recently I've found out that tmp folder can't be used as a persistent storage on Heroku, so I decided to move the file to AWS S3.

That's what I've got so far:

def show
  uploader = PriceUploader.new
  uploader.retrieve_from_store!('price.xls')
end

Now, how do I send the file to the browser?

upd

I itentionally didn't mount the uploader

回答1:

Figured it out.

def show
  uploader = PriceUploader.new
  uploader.retrieve_from_store!('price.xls')
  uploader.cache_stored_file!

  send_file uploader.file.path
end


回答2:

In my case

# find  uploader ...

send_file(uploader.path,
         filename: uploader.filename,
         type: "application/<some-type>")