All threads / Forcing a file to download instead of displaying in the browser

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?

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!

Join the discussion

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

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

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

    logo Created with Sketch.

    Ruby on Rails tutorials, guides, and screencasts for web developers learning Ruby, Rails, Javascript, Turbolinks, Stimulus.js, Vue.js, and more. Icons by Icons8

    © 2020 GoRails, LLC. All rights reserved.