问题
I use "paperclip", "~> 4.1" (on windows 8) to save picture to my product. I have below code:
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
Log of system:
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)
Where is the problem?
And how can I save pdf
file to paperclip
and show it in view?
回答1:
Paperclip introduced spoofing validation at v4.0 to ensure that the contents of the file match the extension.
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.
This little bit from your log:
content type discovered from file command: .
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.
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
回答2:
I tried the solution proposed by user740584 and it did not resolve the problem for me. I created a separate file within the intializers folder called "Disable_Spoofing.rb" and placed the suggested code in there.
The way to solve it for me was to downgrade my version of paperclip. I downgraded from from 4.2.1 to 3.5.3. I then ran "$ bundle install" and then restarted the rails server and I could then upload images successfully.
来源:https://stackoverflow.com/questions/23460108/cannot-save-image-by-paperclip-in-rails-on-windows-8