如何访问使用 Paperclip/Ruby on Rails 上传的文件的原始内容? [英] How do you access the raw content of a file uploaded with Paperclip / Ruby on Rails?

查看:33
本文介绍了如何访问使用 Paperclip/Ruby on Rails 上传的文件的原始内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 Paperclip/S3 上传文件.我上传类似文本的文件(不是 .txt,但它们本质上是 .txt).在显示控制器中,我希望能够获取上传文件的内容,但不将内容视为其属性之一.我可以在这里做什么?

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 - 似乎所有的 Paperclip 教程都是关于图像的,而不是文本文件.

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

推荐答案

以下是我访问附件原始内容的方法:

Here's how I access the raw contents of my attachment:

class Document

  has_attached_file :revision

  def revision_contents
    revision.copy_to_local_file.read
  end

end

请注意,我省略了回形针配置选项和任何类型的错误处理.

Please note, I've omitted my paperclip configuration options and any sort of error handling.

这篇关于如何访问使用 Paperclip/Ruby on Rails 上传的文件的原始内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆