22

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 *.jpg to be uploaded by users. So, in the file selection window they must see only files with the jpg extension.

How can I get this in RoR?

februaryInk
  • 705
  • 1
  • 8
  • 15
Jamal Abdul Nasir
  • 2,557
  • 5
  • 28
  • 47

4 Answers4

22

With HTML5 you can use the :accept for limiting mime-types, like so:

 <%= file_field_tag :csv_file,  :accept => 'text/csv' %>
Roger
  • 7,535
  • 5
  • 41
  • 63
  • What is "csv_file?" Is that built in? – Jubl May 30 '16 at 03:59
  • 1
    @Jubl no thats the name of the field. Have a look at the API http://apidock.com/rails/ActionView/Helpers/FormTagHelper/file_field_tag – Roger Jun 01 '16 at 22:41
  • @Ctpelnar1988 check the API (url above). You can set to accept multiple files (although it depends a bit on your browser) and set to accept one or more mime-types. – Roger Jun 04 '16 at 07:46
11

The answer to this question is probably more related to html uploading than rails.

When you want to upload a file, you typically do an input with type="file".

This can be done in Rails by using the file_field_tag helper. It will generate an input with type="file" which can also have an accept attribute, but you can't really use that because it's not really going to have any visible effect. This attribute accepts MIME types, not extensions, and most browsers don't even use it.

The best thing you can do is probably have a javascript check the file extension before upload (after you select the file from the dialog box). Read more about it in this question.

The point is, you can't force the OS to show you only the file extensions that you want. You can either validate the extension by using JS for example, before upload, or check the contents of the file after upload, server side

Community
  • 1
  • 1
Andrei S
  • 6,486
  • 5
  • 37
  • 54
  • 1
    Using Javascript is not a good idea because it can be easily circumvented. Better go with server-side validation, e.g. as supported by Paperclip. – Mark Jan 22 '11 at 17:08
  • 3
    I'm not recommending the use of javascript for validation, but as a formality only (I mean.. why let someone upload a picture with a wrong extension). If the user uploads a file, even with the required extension, its obviously that it must be checked server-side. but that's not the issue here. the question was if it can be validated before upload (probably to save the time for uploading something that's not valid. at least that's how i see it) – Andrei S Jan 22 '11 at 17:43
8

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

fl00r
  • 82,987
  • 33
  • 217
  • 237
0
<%= file_field_tag :file, accept: 'image/jpg'%>

on rails 5.2

Ankit Wadhwana
  • 325
  • 3
  • 9
  • Instead of just providing code, you should ideally include an explanation of _why_ the code is useful here. That will help reduce any back and forth, and aid new developers in understanding the reasoning, not just the syntax. – Jeremy Caney May 07 '20 at 00:25
  • In addition, I'd recommend checking the existing answers before contributing your own. This appears to largely reiterate what both the accepted answer as well as the top-rated answer suggested—both over seven years ago. – Jeremy Caney May 07 '20 at 00:27