byCoder
byCoder

Reputation: 9184

Ruby save file and encode it

In my rails app i do file-upload, and some of my files are in russian (and other...). If i simply try:

 require 'fileutils'
     uploaded_io = params[:aut]
     File.open(Rails.root.join('public', 'uploads_prices', uploaded_io.original_filename), 'w', :encoding => "r:UTF-8") do |file|
       file.write(uploaded_io.read)
     end

i get error

also if i write

require 'fileutils'
    uploaded_io = params[:aut]
    File.open(Rails.root.join('public', 'uploads_prices', uploaded_io.original_filename), 'wb') do |file|
      file.write(uploaded_io.read)
    end

all is ok, but then, when i parse my csv-files, i need to decode them using option

r:ISO-8859-15:UTF-8

This is not good. So how could i do that if i send my file to method, it save it, but before it converts this file to utf8?

Upvotes: 0

Views: 508

Answers (1)

tadman
tadman

Reputation: 211540

You should be able to re-encode any data using:

uploaded_io.read.encode('UTF-8')

Keep in mind that any characters that can't be re-mapped, for whatever reason, may cause an exception so be sure to test thoroughly.

Upvotes: 1

Related Questions