How do you access the raw content of a file uploaded with Paperclip / Ruby on Rails?

Geoff picture Geoff · Jul 2, 2011 · Viewed 20.7k times · Source

I'm using Paperclip / S3 for file uploading. I upload text-like files (not .txt, but they are essentially a .txt). In a show controller, I want to be able to get the contents of the uploaded file, but don't see contents as one of its attributes. What can I do here?

attachment_file_name: "test.md", attachment_content_type: "application/octet-stream", attachment_file_size: 58, attachment_updated_at: "2011-06-22 01:01:40"

PS - Seems like all the Paperclip tutorials are about images, not text files.

Answer

jwadsack picture jwadsack · Apr 22, 2014

In Paperclip 3.0.1 you could just use the io_adapter which doesn't require writing to (and removing from) the local file system.

Paperclip.io_adapters.for(attachment.file).read