错误,Ruby on Rails:Encoding::UndefinedConversionError in CoursesController#attachment "\xFF"从 ASCII-8BIT 到 UTF-8 [英] Error, Ruby on Rails: Encoding::UndefinedConversionError in CoursesController#attachment "\xFF" from ASCII-8BIT to UTF-8

查看:46
本文介绍了错误,Ruby on Rails:Encoding::UndefinedConversionError in CoursesController#attachment "\xFF"从 ASCII-8BIT 到 UTF-8的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 Rails 3.2.8 上使用 tag_form 制作一个简单的文件上传器.
但是当我尝试提交图像文件时,我收到一条错误消息

错误消息(当我尝试提交图像文件时)

<块引用>

Encoding::UndefinedConversionError in CoursesController#attachment
"\xFF" 从 ASCII-8BIT 到 UTF-8

如果您能帮我解决这个问题,我将不胜感激.
这是我的代码.


app/view/show.html.erb

<%= form_tag(attachment_course_path, :action=>'attachment', :multipart => true) do %><div class="field"><%= label_tag :file %><%= file_field_tag :file %>

<div class="actions"><%= submit_tag '提交' %>

<%结束%>


app/controller/courses_controller.rb

def 附件t = Time.now.strftime("%Y%m%d%H%M%S")上传_io = params[:file]File.open(Rails.root.join('public', 'upload', Uploaded_io.original_filename), 'w') do |file|file.write(uploaded_io.read)结尾结尾


config/routes.rb

resources :courses, :only =>[ :show ] 做会员做帖子:附件结尾结尾

解决方案

尝试以二进制模式打开文件('wb' 而不是 'w'):

<代码> ...File.open(Rails.root.join('public', 'upload', Uploaded_io.original_filename), 'wb') do |file|file.write(uploaded_io.read)结尾

Ruby Docs IO 开放模式

I'd like to make a simple file uploader using tag_form on Rails 3.2.8.
But when I try to submit a image file, I get an error saying

Error Message (when I try to submit a image file)

Encoding::UndefinedConversionError in CoursesController#attachment
"\xFF" from ASCII-8BIT to UTF-8

I'd appreciate it if you help me with this problem.
Here's my codes.


app/view/show.html.erb

<%= form_tag(attachment_course_path, :action=>'attachment', :multipart => true) do %>
<div class="field">
  <%= label_tag :file %>
  <%= file_field_tag :file %>
</div>
<div class="actions">
  <%= submit_tag 'Submit' %>
</div>
<% end %>


app/controller/courses_controller.rb

def attachment
  t = Time.now.strftime("%Y%m%d%H%M%S")
  uploaded_io = params[:file]
  File.open(Rails.root.join('public', 'upload', uploaded_io.original_filename), 'w') do |file|
    file.write(uploaded_io.read)
  end
end


config/routes.rb

resources :courses, :only => [ :show ] do
  member do
    post :attachment
  end
end

解决方案

Try to open the file in binary mode ('wb' instead of 'w'):

  ...
  File.open(Rails.root.join('public', 'upload', uploaded_io.original_filename), 'wb') do |file|
    file.write(uploaded_io.read)
  end

Ruby Docs IO Open Mode

这篇关于错误,Ruby on Rails:Encoding::UndefinedConversionError in CoursesController#attachment &quot;\xFF&quot;从 ASCII-8BIT 到 UTF-8的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆