Lasse Bunk
Lasse Bunk

Reputation: 1858

Setting Content-Disposition to attachment using Ruby on Rails and Paperclip

I have a – hopefully small – problem.

I am using Ruby on Rails and Paperclip to handle file uploads. Now I want to automatically set the Content-Disposition header to "attachment" so that when the user clicks a link, the file is downloaded instead of shown directly in the browser.

I found the following solution for Amazon S3: Download file on click - Ruby on Rails

But I don't use S3.
Can anybody help?

Upvotes: 2

Views: 4595

Answers (2)

Lasse Bunk
Lasse Bunk

Reputation: 1858

According to this link, you can do the following:

<Files *.xls> ForceType application/octet-stream Header set Content-Disposition attachment </Files> 
<Files *.eps> ForceType application/octet-stream Header set Content-Disposition attachment </Files>

Upvotes: 0

hjblok
hjblok

Reputation: 2966

If you use File Storage, Paperclip stores the files within the RAILS_ROOT/public/system folder (configurable using the :path option).

Files from the /public folder are served directly as static files. "Rails/Rack never sees requests to your public folder" (to quote cwninja).

The files from the /public folder are served by the webserver running this app (for example Apache or WEBrick in development). And the webserver is responsible for setting the headers upon serving the file. So you should configure the webserver to set the correct headers for your attachment.

Another option is to build a controller or some Rack middleware to serve your paperclip attachments. There you can do something like response.headers['Content-Disposition'] = 'attachment'.

Third option is to use S3, then you can store headers (like Content-Disposition) within the S3-object. S3 then serves the paperclip attachment using those headers.

Upvotes: 0

Related Questions