[英]rails read xls uploaded file
我已經上傳了.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
但是上載變量我有String,並且我不能使用文件方法。 如何打開已上傳的文件以供roo gem閱讀?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.