Skip to content

Instantly share code, notes, and snippets.

@whalesalad
Created August 20, 2009 00:31
Show Gist options
  • Select an option

  • Save whalesalad/170744 to your computer and use it in GitHub Desktop.

Select an option

Save whalesalad/170744 to your computer and use it in GitHub Desktop.
// Rough method to determining a file's type based upon it's extension.
// Requires jQuery
function getFileType(filename) {
// Get the extension of the file
var extension = filename.split('.').pop().toLowerCase();
// File types and their corresponding possible extensions.
var types = {
'image': ['jpg', 'jpeg', 'gif', 'png', 'bmp'],
'audio': ['wav', 'mp3', 'wma'],
'text': ['doc', 'docx', 'txt'],
'pdf': ['pdf'],
'powerpoint': ['ppt', 'pptx'],
'excel': ['xls', 'xlsx']
};
for (t in types) {
// Check to see if the extension is a member of each key's array... if so, return the key.
if ($.inArray(extension, types[t]) >= 0) { return t; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment