可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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