开发者

How can I specify what files can be uploaded to the server in paperclip

Yes I know that paperclip has a validates_attachment_content_type, but I would really like to have it validate_by_file_extension ... ie ... I have an array of allowed file extensions in my app, and I'd like for paperclip to see if the file about to be uploaded has a file extension in that array, and if not I want it to开发者_运维百科 not even start the upload and kick back an error.

How do I go about doing this.


you can define your own validation methods:

validate :validate_by_file_extension

def validate_by_file_extension
  errors.add_to_base("Invalid file extension") unless ALLOWED_EXTENSIONS.include?(File.extname(attachment_file_name))
end

but you can't easily kick back before the start of the upload from rails, as in most cases by the time your rails controller action method gets called, the file has been streamed and a temp file has been created. You'd need to go higher on the stack to be able to stop things from even starting to upload.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜