我正在尝试保留使用 refile gem 上传到 rails 应用程序的文件的文件名。我在 rails 应用程序中创建了一个 API,以允许通过 curl 上传文件,但是当我有一个文件时,即myfile.csv
我将其上传到 rails 应用程序时,它会以字符散列的名称存储文件,即2HCF3F3...
它也没有t 保留文件扩展名。
我的代码如下所示,
class AddCsvFileToCsvFile < ActiveRecord::Migration
def change
add_column :csv_files, :csv_file_id, :string
add_column :csv_files, :csv_file_filename, :string
add_column :csv_files, :csv_file_content_type, :string
end
end
型号 # csv_file.rb
class CsvFile < ActiveRecord::Base
# validate :csv_extension
validate :file_extension_validation, :csv_size_validation, :if => "csv_file"
# attachment :content_type => "text/csv"
# http://ryanbigg.com/2009/04/how-rails-works-2-mime-types-respond_to/
# attachment :csv, extension: "csv", content_type: "text/csv", raise_errors: true
attachment :csv_file, extension: "csv", content_type: "application/octet-stream", raise_errors: true
private
def file_extension_validation
end
NUM_BYTES_LIMIT = 4000
def csv_size_validation
# binding.pry
errors.add(:csv_file, "file size can't exceed 4KB") if csv_file.size > NUM_BYTES_LIMIT
# binding.pry
end
def csv_file_id
end
# def csv_extension
# unless csv_content_type == "text/csv"
# errors.add :csv, "format must be csv"
# end
# end
# def csv_extension
# unless File.extname(csv_filename) == "csv"
# errors.add :csv, "format must be csv"
# end
# end
end