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
  • 13,005
  • 23
  • 79
  • 119
user2002495
  • 2,126
  • 8
  • 31
  • 61

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
  • 103,943
  • 14
  • 161
  • 157
  • 1
    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