1

Am trying to allow files to be uploaded by having extensions of .xls and .xlsx. So, in the file selection window it must allow only files with these extension.

Using the below code for doing file upload in Rails

  <%= form_for @book, :url => book_path, :method => :post do |f| %>
      <div class="file_input" id="import">
        <%= f.file_field(:excel_file) %>
      </div>
  <% end %>

So, how do i allow files only with these (.xls, .xlsx) extensions ?

diya
  • 6,938
  • 9
  • 39
  • 55

2 Answers2

0

You shouldn't really because it's a security risk. Rather use a gem like Paperclip or Carrierwave. There are Railscasts for both to get you started on using them.

Simpleton
  • 6,285
  • 11
  • 53
  • 87
0

You can pass an accept option to the file_field form helper:

<%= f.file_field(:excel_file, accept: "application/vnd.ms-excel,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet") %>

MDN has a list of common mime types for convenient reference.

lobati
  • 9,284
  • 5
  • 40
  • 61