Ruby on Rails上传文件问题奇怪的utf8转换错误 [英] Ruby on Rails upload file problem odd utf8 conversion error
问题描述
\xFFfrom ASCII- 8BIT到UTF-8
我非常关注他们正在做的事情。这是我使用的代码。
$ p $ file = params [:uploaded_file]
File.open (Rails.root.join('public','images',file.original_filename),'w')do | f |
f.write(file.read)
end
我不得到为什么它不起作用。更新 - 这是应用程序跟踪
app / controllers / shows_controller.rb:16:在block中创建
app / controllers / shows_controller.rb:15 :在`open'
app / controllers / shows_controller.rb:15:在`create'
为了避免编码问题在二进制模式下打开文件,所以你的模式应该是二进制的wb可写:
File.open(Rails.root.join('public','images',file.original_filename),' wb')do | f |
f.write(file.read)
end
I am trying to upload a file and i am getting the following error:
"\xFF" from ASCII-8BIT to UTF-8
I am pretty much following the rails guides in what they are doing. Here is the code I am using.
file = params[:uploaded_file]
File.open(Rails.root.join('public', 'images', file.original_filename), 'w') do |f|
f.write(file.read)
end
I don't get why it doesn't work. What am I doing wrong?
Update -- Here is the application Trace
app/controllers/shows_controller.rb:16:in `write'
app/controllers/shows_controller.rb:16:in `block in create'
app/controllers/shows_controller.rb:15:in `open'
app/controllers/shows_controller.rb:15:in `create'
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
这篇关于Ruby on Rails上传文件问题奇怪的utf8转换错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!