John Isaiah Carmona
John Isaiah Carmona

Reputation: 5366

Rails Rename tempfile

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?

Upvotes: 1

Views: 2179

Answers (2)

Schwern
Schwern

Reputation: 165586

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
)

Upvotes: 0

John Isaiah Carmona
John Isaiah Carmona

Reputation: 5366

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)

Upvotes: 0

Related Questions