Geo*_*off 25 ruby-on-rails paperclip
我正在使用Paperclip/S3进行文件上传.我上传文本类文件(不是.txt,但它们本质上是.txt).在show controller中,我希望能够获取上载文件的内容,但不要将内容视为其属性之一.我能在这做什么?
attachment_file_name: "test.md", attachment_content_type: "application/octet-stream", attachment_file_size: 58, attachment_updated_at: "2011-06-22 01:01:40"
Run Code Online (Sandbox Code Playgroud)
PS - 似乎所有的Paperclip教程都是关于图像,而不是文本文件.
jwa*_*ack 88
在Paperclip 3.0.1中,您可以使用io_adapter不需要写入(和删除)本地文件系统的文件.
Paperclip.io_adapters.for(attachment.file).read
Run Code Online (Sandbox Code Playgroud)
要访问该文件,您可以使用路径方法:http:
csv_file.path
//rdoc.info/gems/paperclip/Paperclip/Attachment#path-instance_method
这可以与例如CSV读取器一起使用.
小智 5
@ jon-m答案需要更新以反映回形针的最新更改,为了使其有效,需要将其更改为以下内容:
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
Run Code Online (Sandbox Code Playgroud)
@jwadsack提到的using Paperclip.io_adapters.for方法有点令人费解,可以达到相同目的,并且似乎是执行此恕我直言的更好,更简洁的方法。
以下是我访问附件原始内容的方法:
class Document
has_attached_file :revision
def revision_contents
revision.copy_to_local_file.read
end
end
Run Code Online (Sandbox Code Playgroud)
请注意,我省略了回形针配置选项和任何类型的错误处理。
| 归档时间: |
|
| 查看次数: |
18730 次 |
| 最近记录: |