irruputuncu
irruputuncu

Reputation: 1490

Ruby on Rails CSV upload&import - File name too long

I want to let admins import contacts via csv files into the database. Therefore I am using the ruby csv library and the following code snippet:

 if request.post? && params[:file].present?
     inputFile = params[:file].read
     CSV.foreach(inputFile) do |row|
         #save row here
     end
 end

However in CSV.foreach(inputFile) do |row| I get an "Errno::ENAMETOOLONG - File name too long"-error and the error message shows me that it uses the whole csv file as file name.

Does anyone know why it does that?

BTW: The csv file is using ',' and '/n' as delimiters.

Upvotes: 2

Views: 7344

Answers (3)

irruputuncu
irruputuncu

Reputation: 1490

Thanks to the input of the other answers I found the solution myself. The problem is that .read turns the file into a string with the contents, but CSV.foreach() expects a filename or path . Using .path instead solves the problem:

 if request.post? && params[:file].present?
     inputPath = params[:file].path
     CSV.foreach(inputPath) do |row|
         #save row here
     end
 end

Upvotes: 11

lhdv
lhdv

Reputation: 156

Try to just remove the .read when you are getting the value from params. Then the variable inputFile may have the path of file to pass to CSV.foreach

Upvotes: 0

challeng
challeng

Reputation: 175

It could have to do with the .read call on line 2. Somehow inputFile is turning into the contents of the csv and not the name of the file itself. That would lead me to believe there is a problem with you setting the inputFile variable. My guess is that .read is not working in the way you intended it to.

Upvotes: 1

Related Questions