2013-02-25 52 views
1

我正在使用file[content]=%FILECONTENTHERE%接收文件。我希望直接收到我的文件,而不需要使用file[content]或任何類型的POST鍵。通過API接收文件內容,無需鍵盤輸入?

目前,我正在做這樣的事情在我的控制器:

def file_from_params 
    return nil if params[:file].blank? || params[:file][:content].blank? 
    temp = Tempfile.new(['import', '.txt']) 
    temp.write params[:file][:content] 
    temp.rewind 
    temp 
end 

我怎樣才能在Rails中實現這一目標?

回答

1

解決使用request.body.read

def file_from_params 
    file = request.body.read 
    return nil if file.blank? 
    temp = Tempfile.new(['import', '.txt']) 
    temp.write file 
    temp.rewind 
    temp 
end