Rails 4,Paperclip 4.2.1给出了二进制文件上传错误 [英] Rails 4, Paperclip 4.2.1 give error with binary file upload

查看:84
本文介绍了Rails 4,Paperclip 4.2.1给出了二进制文件上传错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用滑轨4和回形针4.2.1进行了以下设置

I have following setup with rails 4 and paperclip 4.2.1

class Post< ActiveRecord::Base
  has_attached_file :key
  allowed_content_type = ['text/plain',
'text/rtf', 
'text/richtext',
'application/txt',
'application/octet-stream']
  validates_attachment_content_type :key, :content_type => allowed_content_type, :message=> "Only #{allowed_content_type} is allowed"

我的应用程序中有此文件.rb

I have this in my application.rb

<body data-controller="<%= controller.controller_path %>" data-action="<%= controller.action_name %>" data-no-turbolink="true">
  <%= content_tag "div", id: "params", data: { params: params } do %>
  <% end %>

Post Controller很简单

Post Controller is simple

  def update
  Post.transaction do
      @post.attributes = (artifact_params)
        if @artifact.errors.blank?
        redirect_to(@artifact, :notice => 'Evidence item updated successfully')
      else
        render :action => 'edit'
        raise ActiveRecord::Rollback
      end

它可以与其他所有文件类型完美配合.当我尝试二进制文件时出现错误. 这是错误:

It works flawlessly with all other file types. Errors out when I try binary file. This is the error:

Posts#update中的Encoding :: UndefinedConversionError

Encoding::UndefinedConversionError in Posts#update

app/views/layouts/application.html.erb,其中第58行出现:

app/views/layouts/application.html.erb where line #58 raised:

56: <body data-controller="<%= controller.controller_path %>" data-action="<%= 
57: controller.action_name %>" data-no-turbolink="true">
58: <%= content_tag "div", id: "params" , data: { params: params } do %>
59: <%#= params.inspect %>
60: <% end %> 

在日志中显示:

ActionView::Template::Error ("\xAD" from ASCII-8BIT to UTF-8):
    55: </head>
    56: 
    57: <body data-controller="<%= controller.controller_path %>" data-action="<%= controller.action_name %>" data-no-turbolink="true">
    58:   <%= content_tag "div", id: "params" , data: { params: params } do %>
    59:     <%#= params.inspect %>
    60:   <% end %>
    61:   
  app/views/layouts/application.html.erb:58:in `_app_views_layouts_application_html_erb__387563064_102572910'
  app/controllers/posts_controller.rb:978:in `block in update'
  app/controllers/posts_controller.rb:790:in `update'
 Rendered /home/adminuser/.rvm/gems/ruby-2.1.1/gems/actionpack-4.2.0/lib/action_dispatch/middleware/templates/rescues/_source.erb (15.0ms)
  Rendered /home/adminuser/.rvm/gems/ruby-2.1.1/gems/actionpack-4.2.0/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (7.1ms)
  Rendered /home/adminuser/.rvm/gems/ruby-2.1.1/gems/actionpack-4.2.0/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (1.9ms)
  Rendered /home/adminuser/.rvm/gems/ruby-2.1.1/gems/actionpack-4.2.0/lib/action_dispatch/middleware/templates/rescues/template_error.html.erb within rescues/layout (47.8ms)
Cannot render console with content type multipart/form-dataAllowed content types: [#<Mime::Type:0xa835748 @synonyms=["application/xhtml+xml"], @symbol=:html, @string="text/html">, #<Mime::Type:0xa835608 @synonyms=[], @symbol=:text, @string="text/plain">]

推荐答案

花了整整一天的时间后,我发现这是由回形针中的错误引起的.如果您没有将二进制文件映射到application/octet-stream,则会在尝试将参数转换为视图主体中的json字符串时产生此错误.您必须将任何二进制文件类型映射到application/octet-stream才能消除此错误.

After spending one whole day on this, I figured out that this is caused by a bug in paperclip. If you do not have your binary file mapped to application/octet-stream it produces this error while trying to convert params to json string in the view body. You must map any binary file type to application/octet-stream in order to get rid of this error.

1.因此在config/initializers/中创建paperclip.rb 2.在config/initializers/paperclip.rb中,放置以下代码:

1.So create paperclip.rb in config/initializers/ 2.In config/initializers/paperclip.rb place the following code:

Paperclip.options[:content_type_mappings] = {
  tc: 'application/octet-stream'
}

其中tc是二进制文件的扩展名.我不知道如果您有一个没有扩展名的文件,这将如何工作.回形针的所有者应该清楚地记录下来,以减轻用户的痛苦.

where tc is the extension of your binary file. I don't know how this will work if you have a file without extension though. paperclip owners should document this clearly to save pain to the users.

这篇关于Rails 4,Paperclip 4.2.1给出了二进制文件上传错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆