Reputation: 307
I have uploaded .xls file but I can't read it by roo. In controller I have:
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
In model:
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
But upload variable I have String and I can't use file methods. How I can open already uploaded file for reading by roo gem?
Upvotes: 1
Views: 2682
Reputation: 8954
I described how to work with uploads without using gems here:
https://stackoverflow.com/a/9545030/1001324
You cant work with the original filename on the serveside like you do it.
Upvotes: 1