0

I have the html file input:

<input type='file' id='upload' accept='audio/*'  onchange='getFileType(this);return false;' />

And javascript function:

function getFileType(fl){
   var fileExt = "." + fl.value.match(/\.([^\.]+)$/)[1];
   alert(fileExt);
}

It works good, but if operating system is configured to hide known mime types so file extension is missing. And fl.value.match(/\.([^\.]+)$/)[1] is crushes on null[1] - "Cannot read property '1' of null".

So how I can get file extension on this case? I need cross browsers solution.

Sibeesh Venu
  • 18,755
  • 12
  • 103
  • 140
freethinker
  • 2,257
  • 4
  • 26
  • 49
  • If the system is not giving you a file extension then you cannot get the file extension. Are you sure it does not? Can you console.log(fl) ? Also if it DOES give the extension, then this is a duplicate of http://stackoverflow.com/questions/190852/how-can-i-get-file-extensions-with-javascript - -also look here: http://stackoverflow.com/questions/18299806/how-to-check-file-mime-type-with-javascript-before-upload – mplungjan Nov 29 '16 at 08:31
  • What OS are you referring to? Windows has a similar feature but it works at Explorer level, not file system level. – Álvaro González Nov 29 '16 at 08:53
  • write now is happens on android version 5.1.1. With hidden mime types – freethinker Nov 29 '16 at 09:12
  • 1
    why do you need the file extension? if you want the mimeType, don't trust your user. http://stackoverflow.com/questions/18299806/how-to-check-file-mime-type-with-javascript-before-upload – Kaiido Nov 29 '16 at 09:44

0 Answers0