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

匿名 (未验证) 提交于 2019-12-03 02:33:02

问题:

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.

回答1:

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 


回答2:

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.



回答3:

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



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!