Forcing a file to download instead of displaying in the browser
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"}