【问题标题】:Decoding attachments using Mail Gem and Paperclip使用 Mail Gem 和 Paperclip 解码附件
【发布时间】:2015-06-18 01:28:06
【问题描述】:

我可以通过邮件 gem 通过 imap 接收电子邮件,并希望将附件添加到我的模型中(称为消息)。我收到此错误是基于我从 blog post 获取的代码:

Encoding::UndefinedConversionError ("\xC1" from ASCII-8BIT to UTF-8):

我的代码:

mail.attachments.each_with_index do | attachment, index |
      fake_file = AttachmentFile.new("test.jpg")
      fake_file.write(attachment.decoded)
      fake_file.flush
      fake_file.original_filename = attachment.filename
      fake_file.content_type = "image/gif"

      @message.doc1 = fake_file if index == 0 and attachment.content_type.start_with?("image/")
end

不确定我做错了什么导致错误 - 可能是因为文件不是以二进制模式读取的?同一篇文章中给出了另一种选择:

file = StringIO.new(attachment.decoded)
      file.class.class_eval { attr_accessor :original_filename, :content_type }
      file.original_filename = attachment.filename
      file.content_type = attachment.mime_type

这对 gif 有效,但对 pdf 失败,提供回滚。另外,我在 Windows for Dev 上,这导致过去回形针出现问题(文件大小无法正确读取等)

该模型具有以下验证:

validates_attachment_content_type :doc1, :content_type => [ 'application/pdf', /image/ ], :message => "only pdf or img"

我的日志文件没有那么有用,只有第一个和第二个选项的正常输出:

 Command :: SET PATH=/usr/bin;%PATH% & file -b --mime "C:/Users/FOUNDA~1/AppData/Local/Temp/c81e728d9d4c2f636f067f89cc14862c20150412-5216-1utzh29.gif"

不确定邮件 gem 有什么样的初始化 - 它能够正确提取电子邮件。抱歉,我可能误解了您的回复。

【问题讨论】:

  • 请稍微扩展您的问题。获取错误的堆栈跟踪以及 mail 的初始化方式将很有用。

标签: ruby-on-rails email rubygems paperclip email-attachments


【解决方案1】:

我犯了一个错误,因为验证要求文件名具有特定结构,这导致了问题。我列出的第二个选项现在完美无缺。不能代表第一个 - 可能存在相同的问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-01
    • 2016-01-21
    • 2015-10-23
    • 2016-08-23
    • 1970-01-01
    • 2012-06-06
    相关资源
    最近更新 更多