2013-02-25 94 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 
相关问题