2012-12-14 25 views
5

Tôi đang phát triển ứng dụng Rails 3.2.9 và sử dụng Carrierwave làm trình tải tệp lên. Đọc trên Carriverwave chỉ ra cách lấy đúng content_type:carrierwave content_type luôn luôn nil

  1. Thêm yêu cầu 'carrierwave/processing/mime_types' vào trình khởi tạo hoặc trình tải lên của bạn.
  2. Thêm bao gồm CarrierWave :: MimeTypes vào trình tải lên của bạn.
  3. Thêm quy trình: set_content_type vào (các) người tải lên của bạn.

cơ sở về vấn đề này, tải lên của tôi là dưới đây:

# 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 

Trong mô hình của tôi, gắn kết người tải lên như file:

mount_uploader :file, AttachmentUploader 

Tuy nhiên, tôi luôn luôn có content_type nil sau khi tập tin tải lên:

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> 

Bất kỳ đề xuất nào? Cảm ơn.

PS: Tôi đã thêm gem "mime-types", "~> 1.19" vào số Gemfile của mình.

Trả lời

0

Tôi chỉ cần nhấn chính xác cùng một vấn đề và không thể tìm thấy bản sửa lỗi dễ dàng.

workaround của tôi là để thêm một cột content_type với mô hình và đặt nó trong quá trình tạo/cập nhật với

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

này hoạt động, mặc dù hy vọng vấn đề được cố định.

2

Bạn sẽ cần phải làm theo hướng dẫn đặt ra ở đây: https://github.com/carrierwaveuploader/carrierwave#setting-the-content-type

Thêm mime-loại đá quý, sau đó thiết lập tập tin tải lên của bạn như vậy

require 'carrierwave/processing/mime_types' 

class MyUploader < CarrierWave::Uploader::Base 
    include CarrierWave::MimeTypes 

    process :set_content_type 
end 
1

Đã cùng một vấn đề cố gắng này trong Model của tôi nộp nơi tôi gắn Uploader

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 

Lưu ý: Bạn cần phải có một lĩnh vực mimetype trong bảng

Các vấn đề liên quan