is there a rails method to loop through each line of uploaded file? “each_line” is an IO method but it's not working

狂风中的少年 提交于 2019-12-06 07:56:14

When you post a 'file_field' , the param returned to the controller has some special magic hooked in.

I.e. in your case you could this

<%= "The following file was uploaded #{params[:csv_file].original_filename}" %>
<%= "It's content type was #{params[:csv_file].content_type}" %>
<%= "The content of the file is as follows: #{params[:csv_file].read}" %>

So those are the three special methods you can call on params[:csv_file], or any params posted as the result of a successful 'file_field_tag' or 'f.file_field' in a view

Just remember that those are the three extra special things you can to to a param posted as a result of a file_field:

original_filename

content_type

read

You've clearly figured out how to do the read, the original_filename and content_type may help you out in the future.

EDIT

OK, so all you have is the read method, which will read the contents of the file uploaded.

contents = params[:csv_file].read

So now contents is a string containing the contents of the file, but nothing else is known about that file EXCEPT that it's a csv file. We know that csvs are delimited with '\r' (I think, I've done a lot of work with parsing csv's, but I'm too lazy to go check)

so you could do this:

contents = params[:csv_file].read
contents.split("\r").each do |csvline|
  ???
end

EDIT 2

so here is the take away from this post

When you post a file_field to a controller, the most common thing to do with the contents of the uploaded file is 'read' it into a ruby String. Any additional processing of the uploaded contents must be done on that ruby String returned from the 'read'.

In this particular case, if the uploaded file is ALWAYS a CSV, you can just assume the CSV and start parsing it accordingly. If you expect multiple upload formats, you have to deal with that, for example:

contents = params[:csv_file].read
case params[:csv_file].content_type
when 'txt/csv'
  contents.split("\r").each do |csvline| 
    ???
  end
when 'application/pdf'
  ???
end
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!