Ruby on Rails Download and Import CSV - File Name Too Long

I want admins to import contacts via csv files into the database. Therefore, I use 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 "Errno :: ENAMETOOLONG - the file name is too long" -error, and the error message shows that it uses the entire csv file as the file name.

Does anyone know why he is doing this?

BTW: the csv file uses ',' and '/ n' as delimiters.

+2
source share
4 answers

Thanks to entering 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 file name 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 
+11
source

This may be due to the .read call on line 2. Somehow, the inputFile turns into the contents of csv, and not the name of the file itself. This would make me believe that you have a problem setting the inputFile variable. I assume that .read does not work as you intended.

+1
source

Try simply removing .read when you get the value from the parameters. Then the variable inputFile can have a file path for going to CSV.foreach

0
source

What if i load base64?

 dataBase64 = params[:picture] imagePath = database64.path 

Nothing has happened

0
source

Source: https://habr.com/ru/post/987742/


All Articles