gpt4 book ai didi

ruby-on-rails - 错误,Ruby on Rails: Encoding::UndefinedConversionError in CoursesController#attachment "\xFF"从 ASCII-8BIT 到 UTF-8

转载 作者:行者123 更新时间:2023-12-03 12:01:55 24 4
gpt4 key购买 nike

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

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

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



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

应用程序/ 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 %>

应用程序/ 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

最佳答案

尝试以二进制模式打开文件( 'wb' 而不是 '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 - 错误,Ruby on Rails: Encoding::UndefinedConversionError in CoursesController#attachment "\xFF"从 ASCII-8BIT 到 UTF-8,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13909812/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com