【问题标题】:Creating an UploadedFile Object on rails from parameters从参数创建一个 UploadedFile 对象
【发布时间】:2014-03-22 12:18:20
【问题描述】:

我正在使用邮件服务器向我的应用发出 JSON 请求以通知收到的邮件。处理附件时,它给了我一个原始的:

    a1.name
    # => e.g. 'sample.pdf'
    a1.type
    # => e.g. 'application/pdf'
    a1.content
    # => this is the raw content provided by Mandrill, and will be base64-encoded if not plain text
    # e.g. 'JVBERi0xLjMKJcTl8uXrp/Og0MTGCjQgMCBvY ... (etc)'
    a1.decoded_content
    # => this is the content decoded by Mandrill::Rails, ready to be written as a File or whatever
    # e.g. '%PDF-1.3\n%\xC4\xE5 ... (etc)'

我想做的是把它变成某种适合用 Paperclip 处理的 Rails“文件”对象。

任何想法如何最好地解决这个问题?

非常感谢, 克里斯。

【问题讨论】:

    标签: ruby-on-rails paperclip mandrill


    【解决方案1】:

    我刚刚解决了这个问题,从 SO 上的其他答案拼凑而成。

    file = StringIO.new(a1.decoded_content)
    file.class.class_eval { attr_accessor :original_filename, :content_type}
    file.original_filename = a1.name
    file.content_type = a1.type
    @d = Document.create #Document is my model that has a paperclip attachment called doc
    @d.doc = file
    @d.save
    

    祝你好运!

    【讨论】:

      猜你喜欢
      • 2021-11-15
      • 1970-01-01
      • 2021-03-30
      • 2020-12-21
      • 1970-01-01
      • 2021-12-24
      • 1970-01-01
      • 1970-01-01
      • 2018-08-20
      相关资源
      最近更新 更多