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

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

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

To access the file you can use the path method: csv_file.path http://rdoc.info/gems/paperclip/Paperclip/Attachment#path-instance_method

This can be used along with for example the CSV reader.


@jon-m answer needs to be updated to reflect the latest changes to paperclip, in order for this to work needs to change to something like:

class Document

  has_attached_file :revision

  def revision_contents(path = 'tmp/tmp.any')
    revision.copy_to_local_file :original, path
    File.open(path).read
  end
end

A bit convoluted as @jwadsack mentioned using Paperclip.io_adapters.for method accomplishes the same and seems like a better, cleaner way to do this IMHO.