How to check the type of rails file loaded?

How can I find out what type of file is sent? For example, csv or xls ... Give the code, please ... I get the file like this:

aut_name = uploaded_io.original_filename File.open(Rails.root.join('public', 'uploads_prices', uploaded_io.original_filename), 'wb') do |file| file.write(uploaded_io.read) end as_load(aut_name) 

Maybe MIMO, but how?

+4
source share
2 answers

uploaded_io.content_type contains a file of type MIME.

So:

uploaded_io.content_type == "text/csv"

+21
source

Unfortunately, it (the content_type method) will not work if the user changes the file extension. I tested this in the rails console, and changing the file extension also changed the output of "content_type".

Found this SO question quite useful:

Define file type in Ruby

+2
source

All Articles