Ruby on Rails upload file problem odd utf8 conversion error
I believe this is a change in how rails 3 works with ruby 1.9, since 1.9 supports encodings it will attempt to convert all strings to whatever encoding you have set in your app configuration (application.rb), typically this is 'utf-8'.
To avoid the encoding issue open the file in binary mode, so your mode would be 'wb' for binary writeable:
File.open(Rails.root.join('public', 'images', file.original_filename), 'wb') do |f|
f.write(file.read)
end
getting encoding error cannot convert ascii-8bit to utf-8bit
changed 'w' to 'wb'
if params[:user][:image].present?
uploaded_io = params[:user][:image]
name = "image_" << @user.username << uploaded_io.original_filename
File.open(Rails.root.join('public', 'images','profile',name ), 'wb') do |file|
file.write(uploaded_io.read)
end
end
Encoding error while writing data from excelfile to database (mysql)
You should add this line to the top of your .rb file
# encoding: utf-8
Or you can use this gem
magic_encoding
Related topic:
Add "# coding: utf-8" to all files
Ruby: File.read Error encoding:UTF-8
It seems you are using older Ruby version. Try this instead:
File.read(inputfile, :encoding => "UTF-8").gsub(/<group.*?type=\"public\".*?\/>/, "")
when we import csv data, how eliminate invalid byte sequence in UTF-8
Ruby 1.9 CSV has new parser that works with m17n. The parser works with Encoding of IO object in the string. Following methods: ::foreach, ::open, ::read, and ::readlines
could take in optional options :encoding
which you could specify the the Encoding.
For example:
CSV.read('/path/to/file', :encoding => 'windows-1251:utf-8')
Would convert all strings to UTF-8.
Also you can use the more standard encoding name 'ISO-8859-1'
CSV.read('/..', {:headers => true, :col_sep => ';', :encoding => 'ISO-8859-1'})
Related Topics
Test Whether a Variable Equals Either One of Two Values
Why Pg::Uniqueviolation: Error: Duplicate Key Value Violates Unique Constraint
How to Track Down a Memory Leak in My Ruby Code
(Ruby,Rails) Context of Self in Modules and Libraries...
How to Find If a String Starts with Another String in Ruby
When to Use a Lambda in Ruby on Rails
Where in the Ruby Language Is %Q, %W, etc., Defined
How to Measure the Size of a Ruby Object
Executing User-Supplied Ruby Code on a Web Server
Handling Namespace Models (Classes) in Namespace
How to Share Variables Across My .Rb Files
Transforming Datetime into Month, Day and Year