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
- 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.
- Thêm bao gồm CarrierWave :: MimeTypes vào trình tải lên của bạn.
- 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.