简体   繁体   中英

Carrierwave + Fog(s3). Letting the users to download the file

I am working on a project where I have to provide download links to user for the files which are stored in the s3. Initially I tried,

link_to "Download", @medium.file.url

But this opens the file directly on the browser. When i tried to download an mp4 file, chrome started playing it automatically. I don't want that to happen. So I am using send_file for this task,

def download
  @medium = Medium.find(params[:id])
  send_file @medium.file.url
end

In my local, I have set the storage to file and I have tested this, which works perfectly fine. But on staging, the files are served from s3, I am always getting ActionController::MissingFile . My app is hosted on heroku. I also want to know if using send_file is good choice or if there is a better way of doing this.

config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect'

When i was googling, i found that nginx header setting for sending files should be enabled for production. I added the following line in config/environments/production.rb. Still no luck. I need some help on this. Thanks.

I think this is because S3 wants the file to be played.

I don't have access to my S3 here, but I solved the problem by changing the action for PDF files (which was shown my case) in the S3 control panel.

If you can fix this, I think you can avoid using send_file completely.

Edit: I managed to access S3 now. Go to properties -> Metadata, and add the key

Content-Disposition: Attachment

Then your file will always be downloaded instead of shown.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM