ruby-on-rails-4file-uploadtemporary-filesroo-gem

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?


Solution

  • 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)