【发布时间】:2014-01-23 12:51:48
【问题描述】:
我已上传 .xls 文件,但 roo 无法读取它。 在控制器中我有:
def create
name = params[:upload][:file].original_filename
directory = "public/uploads"
path = File.join(directory, name)
file = File.open(path, "wb") { |f| f.write(params[:upload][:file].read) }
flash[:notice] = "File uploaded"
file.inspect
Product.import(params[:upload][:file].original_filename)
redirect_to upload_path
end
在模型中:
def self.import(file)
spreadsheet = open_spreadsheet(file)
end
def self.open_spreadsheet(upload)
directory = "public/uploads"
file = File.join(directory, upload)
case File.extname(file.original_filename)
when ".csv" then Csv.new(file.path, nil, :ignore)
when ".xls" then Excel.new(file.path, nil, :ignore)
when ".xlsx" then Excelx.new(file.path, nil, :ignore)
else raise "Unknown file type: #{file.original_filename}"
end
end
但是上传变量我有字符串,我不能使用文件方法。如何打开已上传的文件以供 roo gem 阅读?
【问题讨论】:
标签: ruby-on-rails xls