1

I am using roo-rb for accessing the uploaded files. My code is like this:

s = Roo::Excelx.new(params[:upload][:file].tempfile.path)

But I am having problem with it because the generated tempfile has no extension and I'm having this exception:

.../AppData/Local/Temp/RackMultipart20150216-10192-13yn50s is not an Excel-xlsx file

Is there a way to rename the tempfile.path so that it will have a proper extension (xlsx)? Or is there a more elegant way to solve this problem?

John Isaiah Carmona
  • 5,260
  • 10
  • 45
  • 79

2 Answers2

0

I have successfully renamed a temporary file (even when deployed on Heroku) using the fileutils. Here is the code:

require 'roo'
require 'fileutils'

tmp = params[:upload][:file].tempfile
file = File.join("public", params[:upload][:file].original_filename)
FileUtils.cp tmp.path, file

s = Roo::Excelx.new(file)
John Isaiah Carmona
  • 5,260
  • 10
  • 45
  • 79
0

Or is there a more elegant way to solve this problem?

Yes, you can specify the extension.

s = Roo::Spreadsheet.open(
  params[:upload][:file].tempfile.path,
  extension: :xlsx
)
Schwern
  • 153,029
  • 25
  • 195
  • 336