簡體   English   中英

無法使用回形針從API保存圖像

[英]Can't save image from API with paperclip

我想將我的React應用中的圖像保存在我的Rails應用中。 我將此發布到我的api: 在此處輸入圖片說明

當我嘗試保存它時,出現此錯誤:

{  
   "file":[  
      "has contents that are not what they are reported to be",
      "file type is not acceptable"
   ],
   "file_content_type":[  
      "file type is not acceptable"
   ]
}

我的模型如下所示:

class Photo < ApplicationRecord
  validates :title, presence: true

  belongs_to :user

  has_attached_file :file, styles: { square: "350x233#", medium: "300x300>", thumb: "100x100>" }, default_url: "/images/:style/missing.png"
  validates_attachment_content_type :file, content_type: /\Aimage\/.*\z/, message: 'file type is not acceptable'
  validates_attachment_size :file, less_than: 10.megabytes, message: 'file size is more than 50MB'
end

和我的控制器:

def create
    uploaded_file = false

    if params[:photo][:image]
      tempfile = Tempfile.new(params[:photo][:filename])
      tempfile.binmode
      tempfile.write(Base64.decode64(params[:photo][:image]))

      uploaded_file = ActionDispatch::Http::UploadedFile.new(
        :tempfile => tempfile,
        :filename => params[:photo][:filename],
        :original_filename => params[:photo][:filename],
        :content_type => Mime::Type.lookup_by_extension(File.extname(params[:photo][:filename])[1..-1]).to_s
      )

      params[:photo][:file] = uploaded_file
    end

    params[:photo][:title] = params[:photo][:filename] if params[:photo][:title] == ""

    # @picture = Photo.new(photo_params)
    @photo = current_user.photos.build photo_params
    @photo.save

    render json: @photo.errors
  end

在我的日志中,我發現了這一點:

Unpermitted parameters: :image, :filename
Command :: file -b --mime '/tmp/homepage.jpg20180415-7-1wa7yr3'
[paperclip] Trying to link /tmp/homepage.jpg20180415-7-1wa7yr3 to /tmp/ba3988db0a3167093b1f74e8ae4a8e8320180415-7-jpgu5m.jpg
[paperclip] Trying to link /tmp/ba3988db0a3167093b1f74e8ae4a8e8320180415-7-jpgu5m.jpg to /tmp/ba3988db0a3167093b1f74e8ae4a8e8320180415-7-147ntu1.jpg
Command :: file -b --mime '/tmp/ba3988db0a3167093b1f74e8ae4a8e8320180415-7-147ntu1.jpg'
[paperclip] Content Type Spoof: Filename homepage.jpg (application/octet-stream from Headers, ["image/jpeg"] from Extension), content type discovered from file command: application/octet-stream. See documentation to allow this combination.
  [1m[35m (1.5ms)[0m  [1m[35mBEGIN[0m
[paperclip] Trying to link /tmp/ba3988db0a3167093b1f74e8ae4a8e8320180415-7-jpgu5m.jpg to /tmp/ba3988db0a3167093b1f74e8ae4a8e8320180415-7-ndp9hd.jpg
Command :: file -b --mime '/tmp/ba3988db0a3167093b1f74e8ae4a8e8320180415-7-ndp9hd.jpg'
[paperclip] Content Type Spoof: Filename homepage.jpg (application/octet-stream from Headers, ["image/jpeg"] from Extension), content type discovered from file command: application/octet-stream. See documentation to allow this combination.

我沒弄錯。 有人可以幫我上傳嗎? 我認為content_type丟失了,但是我不知道在哪里。

您是否嘗試過使用Paperclipio_adapter將base64解碼為附件對象? 類似於以下內容:

def create
    file = Paperclip.io_adapters.for(params[:photo][:image])
    file.original_filename = params[:photo][:filename]
    @photo = Photo.new(file: file, user: current_user)
    @photo.save
end

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM