无法通过回形针在 Windows 8 上的导轨中保存图像 [英] cannot save image by paperclip in rails on windows 8
问题描述
我使用回形针"、~> 4.1"(在 Windows 8 上)将图片保存到我的产品中.我有以下代码:
I use "paperclip", "~> 4.1" (on windows 8) to save picture to my product. I have below code:
products_controller:
products_controller:
class ProductsController < ApplicationController
before_action :set_product, only: [:show, :edit, :update, :destroy]
def index
@products = Product.all
end
def show
end
def new
@product = Product.new
end
def edit
@product = Product.all
end
def create
@product = Product.new(product_params)
respond_to do |format|
if @product.save
format.html { redirect_to @product, notice: 'Product was successfully created.' }
format.json { render action: 'show', status: :created, location: @product }
else
format.html {
render action: 'new'
}
format.json { render json: @product.errors, status: :unprocessable_entity }
end
end
end
def update
respond_to do |format|
if @product.update(product_params)
format.html { redirect_to @product, notice: 'Product was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: @product.errors, status: :unprocessable_entity }
end
end
end
def destroy
@product.destroy
respond_to do |format|
format.html { redirect_to products_url }
format.json { head :no_content }
end
end
private
def set_product
@product = Product.find(params[:id])
end
def product_params
params.require(:product).permit(:name, :code,:picture)
end
end
product.rb
class Product < ActiveRecord::Base
has_attached_file :picture, :styles => { :medium => "300x300>", :thumb => "100x100>" }, :default_url => "/images/missing.png"
validates_attachment_content_type :picture, :content_type => [ 'image/gif', 'image/png', 'image/x-png', 'image/jpeg', 'image/pjpeg', 'image/jpg' ]
end
但是当我想保存我上传的图片时,出现以下错误:
but when I want save the picture that I upload, I get below error:
1 error prohibited this product from being saved:
Picture has an extension that does not match its contents
系统日志:
Started POST "/products" for 127.0.0.1 at 2014-05-04 23:23:31 +0430
Processing by ProductsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"XVKgrUjhmB6auJXM5zTrDFrw7GLS0jdUPFelXt4se4Y=", "product"=>{"name"=>"Product 453", "code"=>"324324", "category_id"=>"10", "describe"=>"", "picture"=>#<ActionDispatch::Http::UploadedFile:0x00000004fdb750 @tempfile=#<Tempfile:C:/Users/MGH~1.119/AppData/Local/Temp/RackMultipart20140504-5856-p42svx>, @original_filename="pic.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"product[picture]\"; filename=\"pic.jpg\"\r\nContent-Type: image/jpeg\r\n">}, "commit"=>"Save change"}
User Load (1.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = 17 ORDER BY "users"."id" ASC LIMIT 1
Command :: file -b --mime-type "C:/Users/MGH~1.119/AppData/Local/Temp/be0933fb4765581454c720c6cac4755920140504-5856-15ak6qf"
[paperclip] Content Type Spoof: Filename pic.jpg (["image/jpeg"]), content type discovered from file command: . See documentation to allow this combination.
Responsibility Load (0.0ms) SELECT "responsibilities".* FROM "responsibilities" WHERE "responsibilities"."user_id" = ? ORDER BY "responsibilities"."id" ASC LIMIT 1 [["user_id", 17]]
(1.0ms) begin transaction
Command :: file -b --mime-type "C:/Users/MGH~1.119/AppData/Local/Temp/6db10b0624aa4c70237ca9e622c03f4620140504-5856-t1i2gw"
[paperclip] Content Type Spoof: Filename pic.jpg (["image/jpeg"]), content type discovered from file command: . See documentation to allow this combination.
(0.0ms) rollback transaction
Category Load (0.0ms) SELECT "categories".* FROM "categories"
Rendered products/_form.html.erb (2.0ms)
Rendered products/new.html.erb within layouts/product (3.0ms)
Rendered layouts/_header.html.erb (0.0ms)
Rendered layouts/_sidebar.erb (0.0ms)
Completed 200 OK in 159ms (Views: 16.0ms | ActiveRecord: 2.0ms)
问题出在哪里?
我如何将 pdf
文件保存到 paperclip
并在视图中显示?
And how can I save pdf
file to paperclip
and show it in view?
推荐答案
Paperclip 在 v4.0 中引入了欺骗验证,以确保文件的内容与扩展名匹配.
Paperclip introduced spoofing validation at v4.0 to ensure that the contents of the file match the extension.
它使用 file
命令来确定文件的 MIME 类型,如果您使用 linux 或 OS X,这很好.不幸的是,Windows 没有 file
命令所以它返回空白 - 因为根据其扩展名,这与文件的预期 MIME 类型不匹配,您会收到欺骗错误.
It uses the file
command to determine the MIME type of the file which is fine if you use linux or OS X. Unfortunately Windows doesn't have a file
command so it comes back with blank - since this doesn't match the expected MIME type of the file according to its extension you get the spoofing error.
你的日志中的这一点:
从文件命令发现的内容类型:.
冒号和句号之间的位是 file
命令的输出;在 Windows 上总是空白.
The bit between the colon and the full stop is the output from the file
command; which on Windows is always going to be blank.
我所知道的针对 Windows 的最佳解决方法是禁用欺骗.为此,您需要在初始化程序中放入类似的内容:
The best workaround I know for Windows is to disable spoofing. For this you need to put something like this in an initializer:
module Paperclip
class MediaTypeSpoofDetector
def spoofed?
false
end
end
end
这篇关于无法通过回形针在 Windows 8 上的导轨中保存图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!