0

I have a form that uploads file with ajax, the file selection event is like this:

# This JS is translated directly from coffeescript

$("input[name=dump_file]").on('change', function() {
  # if not .xls do something like window.alert
  # else
  var dump_file;
  return dump_file = this.files[0];
});

I need to check if dump_file is in excel format (.xls or .xlsx), how to check for this.files[0] file type on change event?

Niklas
  • 12,276
  • 21
  • 71
  • 114
user2002495
  • 2,006
  • 7
  • 28
  • 57

1 Answers1

0

Check the file type included on the file object:

if (this.files[0].type == "xls/xlsx") { //not sure if that's the right type, you'll have to log it first
    //do stuff
}
tymeJV
  • 99,730
  • 13
  • 150
  • 152
  • that returns `application/vnd.openxmlformats-officedocument.spreadsheetml.sheet` I've found solution using this though: `this.files[0].name.split(".").pop()` – user2002495 May 28 '14 at 14:16
  • The above only applies is the file has a single "." in it. – DevlshOne Feb 26 '15 at 14:45