2015-08-24 85 views
0

我試圖按照載波中的「安全上載」,這有點令人困惑,因爲我已經自定義了文件路徑和所有的一點。當我嘗試運行該應用程序時,出現「無法讀取文件」錯誤。載波寶石安全文件路徑無法讀取文件路徑錯誤

,路線如下:

match "/upload_files/:tenant_id/:model/:mount_as/:id/:basename.:extension" => "documents#download",via: [:get, :post] 

class ImageUploader < CarrierWave::Uploader::Base 
    def store_dir 
"upload_files/#{model.tenant_id}/#model.class.to_s.underscore}/#mounted_as}/#{model.id}" 

末 結束

carrierwave.rb初始化:

CarrierWave.configure do |config| 
    config.permissions = 0600 
    config.directory_permissions = 0700 
    config.root = Rails.root 
end 

文件控制器:`

def download 
    path = request.fullpath 
    send_file path 
end 

得到的錯誤

的ActionController :: MissingFile在DocumentsController#下載 無法讀取文件/upload_files/1/hoshin_attachment/image/3/support3_HoshinUserStatusReports_08_14_2015.pdf 請幫我找到解決辦法

回答

0

CarrierWave似乎計算路徑使用變量和上傳者store_dir。 我想將我的文件存儲在Rails.root下的私人文件夾中。 要設置根:

# config/initializers/carrierwave.rb 

# Uploader settings 
CarrierWave.root = Rails.root.to_s 
CarrierWave::Uploader::Base.root = Rails.root.to_s 

要設置store_dir:

# In your uploader class definition 
def store_dir 
    "private/your_app/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}" 
end 

由於文件不再在公共文件夾,它們需要在控制器動作來顯示和下載。讓我們假設上傳文件的列名爲uploaded_file

# In some controller 
# you have to ensure @document initialize with a before_action filter 


def show_file 
    send_file @document.unsigned_file.path, 
      filename: @document.uploaded_file_identifier, 
      disposition: :inline, 
      type: @document.uploaded_file.content_type 
end 

def download_file 
    send_file @document.unsigned_file.path, 
      filename: @document.uploaded_file_identifier, 
      disposition: :attachment, 
      type: @document.uploaded_file.content_type 
end