Rails 3: Upload csv file with non-english characters
I've a CSV file. If the file contains only english characters everything works fine, file uploads and contains data. But, if the file contains non-english characters, it's uploads but uploaded file is empty.
Hope, anybody can help.
UPDATE:
Background: Rails: rails 3.0.9, Ruby: ruby 1.9.2
Form:
<%= form_tag({:action => :import}, :multipart => true) do |f| %>
<%= collection_select(:contact, :list_id, current_user.lists, :id, :title) %>
<%= file_field_tag 'upload' %>
<%= submit_tag("Import") %>
<% end %>
Controller:
def import
begin
Contact.upload(params[:upload], current_user, params[:contact][:list_id])
redirect_to :action => "index"
rescue Exception => e
end
end
Model:
def upload(uploaded_io, user_id, list_id)
File.open(Rails.root.join('public', 'up开发者_JS百科loads', uploaded_io.original_filename), 'w') do |file|
file.write(uploaded_io.read)
end
self.import(uploaded_io.original_filename, user_id, list_id)
end
check this library, it deals with different encodings: Iconv
I think you should use it in your upload
method (with IO operations)
I've resolve a problem. It starts working as expected then I've change 'w' to 'wb' for opening file in a binary mode.
Thank you! Hope its help someone else.
精彩评论