Skip to main content

Forcing a file to download instead of displaying in the browser

General • Asked by Tony

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?


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


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"}


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


Login or Create An Account to join the conversation.

Subscribe to the newsletter

Join 27,623+ developers who get early access to new screencasts, articles, guides, updates, and more.

    By clicking this button, you agree to the GoRails Terms of Service and Privacy Policy.

    More of a social being? We're also on Twitter and YouTube.