2014-02-21 21 views
0

在rails上創建一個UploadedFile對象我正在使用一個郵件服務器,它向我的應用發送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)' 

我想要做的就是把它變成某種Rails的文件「對象,是適合用回形針過程。

任何想法如何最好地解決這個問題?

非常感謝, 克里斯。

回答

0

我剛剛解決了這個問題,把它從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 

祝你好運!