Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check in rails uploaded file type?

How can i see what type of file is comming? For example, csv or xls... Give code please... I get file so:

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 by MIMO, but how?

like image 850
Valdis Azamaris Avatar asked Jan 21 '13 20:01

Valdis Azamaris


2 Answers

uploaded_io.content_type contains the MIME type of file.

So:

uploaded_io.content_type == "text/csv"

like image 99
Daniel Evans Avatar answered Oct 21 '22 18:10

Daniel Evans


Unfortunately, it (content_type method) is not going to work if a user changes file extension. I've tested that in rails console and the changing the file extension also changes "content_type" output.

Found this SO question quite helpful:

Determine file type in Ruby

like image 4
wondersz1 Avatar answered Oct 21 '22 18:10

wondersz1