0

I am trying to read a form-uploaded .csv file. I am taking my answers in part from several answers: In Ruby, how to read data column wise from a CSV file?, how to read a User uploaded file, without saving it to the database, and Rails - Can't import data from CSV file. But so far nothing has worked.

Here is my code:

def upload_file
  file = Tempfile.new(params[:search_file])
  csv_text = File.read(file)
  csv = CSV.parse(csv_text, :headers => true)
  csv.each do |row|
    puts row
  end
  render json: {success: true}
end

I am sure that the file is not nil. It contains 4 columns and 2 rows of simple text. However, my file value above comes out as an empty array, and the csv_text value is an empty string. I am very sure the file contains values.

I have also tried params[:search_field].read and that throws an error every time, saying "undefined method 'read'".

How can I simply read these values from the user uploaded file? I am on rails 5.1.6 and ruby 2.3.

Edit: I have tried some of the solutions below. However, the problem is that it doesn't write the contents of the file, when I call file.write--it simply writes the name of the file (like, myFileNameHere.csv) as a string to the temp file. The "ok testing now" never prints to terminal in the below code. Here is my code now:

file = Tempfile.new(['hello', '.csv'])
file.write(params[:search_file])
file.rewind
csv_text = file.read
csv = CSV.parse(csv_text, :headers => true)
csv.each do |row|
  puts "ok testing row"
  puts row
end
file.close
file.unlink    # deletes the temp file
jjjjjjjj
  • 4,203
  • 11
  • 53
  • 72
  • 1
    I'm betting your *undefined method 'read'* error message says more than what you wrote here. Please indicate the *entire* error message, not just a portion of it. I suspect it says something about 'read' not being defined for a `nil` object or something like that. Check the value of `params[:search_file]`. It's probably `nil` or a file that doesn't exist. – lurker Apr 07 '18 at 12:49
  • What is `params[:search_field]`? – Tom Lord Apr 07 '18 at 13:27
  • Hi @joey, could you show me the input field in the form? – fongfan999 Apr 07 '18 at 14:26

2 Answers2

0

You are reading from a empty tempfile. When you put params[:search_file], this value will become part of the new Tempfile filename (like this "/tmp/#{params[:search_file]}.24722.0").

So when you do File.read(file) it will try to read a tempfile that has params[:search_file] value in the it's filename but has no other value from the form inside it.

You should either skip the Tempfile part and load the params[:search_file] with File.read(params[:search_file]) or fill the new Tempfile object with params[:search_file] content. (I would recommend the first).

0

Tempfile.new('something') always returns an empty temporary file with 'something' in its basename.

First you create the tempfile (with the filename you want), then you can write the content from params[:search_file], rewind and read it.

Source : Class: Tempfile (Ruby 1.9.3)

Sovalina
  • 5,410
  • 4
  • 22
  • 39
  • I have tried this, but the problem is writing the content via params[:search_file] simply writes it as a string to the file. I edited my question above with new code. – jjjjjjjj Apr 08 '18 at 18:57