Ruby on Rails CSV上传和导入-文件名太长 [英] Ruby on Rails CSV upload&import - File name too long
问题描述
我想让管理员通过csv文件将联系人导入数据库。因此,我正在使用ruby csv库和以下代码片段:
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
但是在 CSV.foreach(inputFile)做|行|
我收到 Errno :: ENAMETOOLONG-文件名太长的错误,错误消息显示它使用了整个csv文件作为文件名。
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:csv文件使用的是,和 / n'作为定界符。
BTW: The csv file is using ',' and '/n' as delimiters.
推荐答案
感谢其他答案的输入,我自己找到了解决方案。问题是 .read
会将文件转换为包含内容的字符串,但是 CSV.foreach()
期望文件名或路径。使用 .path
可以解决问题:
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
这篇关于Ruby on Rails CSV上传和导入-文件名太长的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!