【问题标题】:Encoding::UndefinedConversionError -ASCII-8BIT to UTF-8 ruby on railsEncoding::UndefinedConversionError -ASCII-8BIT to UTF-8 ruby​​ on rails
【发布时间】:2015-09-18 14:33:49
【问题描述】:

2.2 和 raisl 4.1。我试图从用户那里读取一个 xlsx 文件。

<%= form_for(@attachment, :html => { :multipart => true,:class => 'form-horizontal' }) do |f| %>
  <div class="form-group">
    <label for="inputEmail3" class="col-sm-2 control-label">List of Company Name</label>
    <div class="col-sm-10">
        <%= f.file_field :attachment,class: 'form-control' %>
    </div>
  </div>
  <div class="form-group">
    <div class="col-sm-offset-2 col-sm-10">
      <%= f.submit 'Import Company Name list',:class => "btn btn-primary" %>
    </div>
  </div>
<% end %>

在控制台中

 Parameters: {"utf8"=>"✓", "authenticity_token"=>"BJMhP6MnjUH17esTRxtaViIpBXL/aMzQgzuOI0a9LjI=", "attachment"=>{"attachment"=>#<ActionDispatch::Http::UploadedFile:0x00000006cd25e8 @tempfile=#<Tempfile:/tmp/RackMultipart20150701-9558-59shsp>, @original_filename="Book2.xlsx", @content_type="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", @headers="Content-Disposition: form-data; name=\"attachment[attachment]\"; filename=\"Book2.xlsx\"\r\nContent-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet\r\n">}, "commit"=>"Import Company Name list"}

即使参数以 UTF-8 标记传递,我也得到了

Encoding::UndefinedConversionError - "\xA0" from ASCII-8BIT to UTF-8:

error.. 尝试了不同的解决方案,例如 .force_encoding('UTF-8') mime::type 但没有成功?

有人知道这个问题吗?

【问题讨论】:

    标签: ruby-on-rails ruby file-upload encoding utf-8


    【解决方案1】:

    我找到了发生这种情况的原因,我有以下

    gem 'meta_request','0.2.1'
    

    在我的 gemfile 中并删除此 gem 使文件上传完美。

    希望这对其他人有所帮助...

    【讨论】:

      猜你喜欢
      • 2015-08-10
      • 2021-05-19
      • 2014-06-12
      • 2014-10-15
      • 1970-01-01
      • 2017-04-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多