2011-02-25 32 views
1

簡單存儲基於CouchPotato的頂部用於處理rails中的CouchDb。在嘗試將文件上傳到couchdb的時候,我們嘗試了使用base64,json post以及沒有任何東西可以正常工作。我們正嘗試上傳到已存儲文檔的_attachments屬性。Rails + CouchDb二進制文件上傳到數據庫

有了這樣的模式:

class Patient 
    include SimplyStored::Couch 
end 

,並在控制器同時接收文件低谷更新操作

def update 
    @patient = Patient.find(params[:id]) 
    if params[:patient][:_attachments] 
     attachement = params[:patient][:_attachments] 
     filedata = attachement.tempfile.read 
     data = Base64.encode64(filedata).gsub(/\n/, '') 
     type = attachement.content_type 
     or_name = attachement.original_filename 
     @patient._attachments = {or_name => {'data' => data, 'content_type' => type}} 
     @patient.save 
     return render :json => @patient._attachments 
    end 
end 

現在,有趣的是,我可以看到,@ patient._acttachments具有文件本身,那是在.save之後渲染中返回的內容;但實際上並沒有將它保存在couchdb數據庫中。

任何想法爲什麼不做保存,或者我應該試着將_attachment推送到couchdb數據庫。 ? (順便總是返回一個500錯誤:()

回答

3

這是非常簡單的,基於couchpotato網站上的解決方案,你其實並不需要將其轉換爲Base64這裏的代碼工作

if params[:patient][:_attachments] 
      attachement = params[:patient][:_attachments] 
      data = attachement.tempfile.read 
      type = attachement.content_type 
      or_name = attachement.original_filename 
      params[:patient][:_attachments] = {or_name => {'data' => data, 'content_type' => type}} 
end 
    if @patient.update_attributes(params[:patient]) #blah blah blah 

因爲值是例子在[:patient] [:_ attachments] params中,你只需要將它作爲另一個參數進行測試和工作。

此外,需要說明的是需要定義你的病人模型

property :_attachments 

說不上來,但我做到了。

我知道我不應該問爲了錢,而是因爲我的工作FOUR你是唯一的100比索/小時..看到你在辦公室

歡呼

LOLS

+0

我希望他至少邀請了幾個啤酒在這:) – Duopixel 2012-10-11 14:33:21

0

我不知道Ruby和couchpotato,但我不認爲你需要Base64你的附件,只是讀取二進制信息並將其寫入請求 我的2分錢小費。:)

+0

產生無效的JSON指令它只是不解析。 – 2011-02-25 22:05:48