2012-12-14 34 views
5

我正在開發Rails 3.2.9應用程序,並使用Carrierwave作爲文件上傳器。 Carriverwave自述指出了獲取正確content_type的方法:carrierwave content_type always nil

  1. 向初始化程序或上傳程序添加要求的'carrierwave/processing/mime_types'。
  2. 將CarrierWave :: MimeTypes添加到您的上傳器中。
  3. 向您的上傳者添加進程:set_content_type。在此

基地,我上傳低於:

# encoding: utf-8 
require 'carrierwave/processing/mime_types' 
class AttachmentUploader < CarrierWave::Uploader::Base 
    include CarrierWave::MimeTypes 
    storage :file 
    def store_dir 
    "#{base_store_dir}/#{model.id}" 
    end 
    process :set_content_type 

end 

在我的模型,安裝上載的文件:上傳文件後

mount_uploader :file, AttachmentUploader 

不過,我總是得到CONTENT_TYPE零:

1.9.3-p327 :013 > a.file.class 
=> AttachmentUploader 
1.9.3-p327 :010 > a.file.file 
=> #<CarrierWave::SanitizedFile:0x00000004046330 @file="uploads/course/000/000/026/attachment_file/6/myIcon.png", @original_filename=nil, @content_type=nil> 

有什麼建議嗎?謝謝。

PS:我已在Gemfile中加入。

回答

0

我剛剛遇到完全相同的問題,無法找到簡單的修復方法。

我的解決方法是一個CONTENT_TYPE列在創建/更新過程中添加到模型,並將其設置與

@model.content_type = params[:file_upload][:attachment].content_type 

這工作,但希望問題得到解決。

1

有同樣的問題在我的模型嘗試這個文件在那裏我登上上傳

before_save :set_mime_type     

    def set_mime_type 
     self.mimetype = Mime::Type.lookup_by_extension(File.extname(self.cf_filename.to_s)[1..-1]) 
    end 

注意:您必須在表中的MIME類型字段

相關問題