Restrict file upload to some file extensions

后端 未结 4 1717
情书的邮戳
情书的邮戳 2021-02-12 15:36

I am having a problem with uploading files. I want to allow users to upload files that the system allows...

For example, I allow files having an extension of *.j

4条回答
  •  情歌与酒
    2021-02-12 16:07

    Firstly you can use extname method to validate files you are saving. http://apidock.com/ruby/File/extname/class

    Secondly I use Paperclip gem https://github.com/thoughtbot/paperclip for uploading files. There is validate_attachment_content_type method for validating extensions: http://rdoc.info/gems/paperclip/2.3.8/Paperclip/ClassMethods#validates_attachment_content_type-instance_method

提交回复
热议问题