Ask A Question

Notifications

You’re not receiving notifications from this thread.

Forcing a file to download instead of displaying in the browser

Tony asked in General

I've got a website where users can upload things such as a mp3 and the files are being stored on Amazon S3.

If a user clicks the file it just seems to open as a streaming file in the browser. I can't work out how I'd need to change my system so that it will download the file instead. Any ideas?

Reply

So browser these days try to be helpful and not force downloads by default. The way you can get the old functionality is to set the Content-Disposition header on the request to attachment which will cause the browser to download the file instead of showing it embedded on the page.

I'm not entirely sure how you'll need to go about that, but it looks like you're able to add the header to S3 requests. http://docs.aws.amazon.com/AmazonS3/latest/API/RESTObjectGET.html

Reply

Thanks.

That was quite a lot easier than the conflicting methods I read when I googled. Just adding the following bit to has_attached_file in the model seems to have done the trick.

:s3_headers => {"Content-Disposition" => "attachment"}

Reply

Oh that's a super simple solution for paperclip. I love it!

Reply
Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 86,796+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.