错误,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
问题描述
我想在 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)结尾
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 on Rails:Encoding::UndefinedConversionError in CoursesController#attachment "\xFF"从 ASCII-8BIT 到 UTF-8的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!