Hi,
I’m having users upload CSV files, but find a problem with files
having different encodings. My default is UTF-8, so when users upload
a ISO-8859-1 encoded file, some characters get munged.
I have a standard file upload field in my view (the form is for an
object “import”):
<%= f.file_field :file %>
And in my controller I pass the file param to the model CsvFile:
@csv_file = CsvFile.new(params[:import][:file])
@csv_file.import
In my model, I first write the file to disk and then read it using
FasterCSV:
def initialize(file)
@file = file
end
def import
write_file
FasterCSV.foreach(tmp_file) do |row|
…
end
end
def write_file
self.tmp_file = “#{RAILS_ROOT}/tmp/csv_files/” +
rand(9999999999).to_s
if file
File.open(tmp_file, “w”) do |f|
f.write(file.read)
end
end
end
I would like to convert it to UTF-8 before saving, changing the
write_file like this:
was: f.write(file.read)
now becomes:
f.write(Iconv.iconv(“UTF-8”, charset, file.read))
where charset is the charset of the uploaded file. But I need to find
out what it is for the conversion to work. It works when I use
explicit “ISO-8859-1”, but then only if the uploaded file is actually
an ISO-8859-1 file. I need to make it variable, because in many cases,
the file will just be UTF-8. Anyone ideas?