20

i want to check the type of uploaded file. If name like example.txt, i want to get the .txt part only. How can i achieve it using Jquery or javascript.

Any Suggestions or links would be appreciative!!!

IamIronMAN
  • 1,871
  • 6
  • 22
  • 28
  • 4
    The *name* of a file may tell you **nothing** about what it actually contains. – Pointy Nov 12 '10 at 13:10
  • have a look at this http://stackoverflow.com/questions/1818310/regular-expression-to-remove-a-files-extension – ArK Nov 12 '10 at 13:13
  • @Pointy: what is it about? I am not able to understand. Are you talking about security threats!!! – IamIronMAN Nov 12 '10 at 15:51
  • 1
    The point is that the name of a file can *easily* be changed by *anybody* so that it's got any extension desired. Whether that is any sort of security threat depends on what your server intends to do with the file. – Pointy Nov 12 '10 at 16:27
  • @Pointy: +1. I have to learn a lot. Thanks! – IamIronMAN Nov 12 '10 at 17:31
  • @Pointy: Can you tell me some Image verification classes in Java. Because i want to validate the uploaded images in server side too!!! –  Nov 12 '10 at 22:57
  • Well I don't know much about doing it in any pure Java way. In my app the way we deal with images is to hand them over to "graphicks magic" tools, which is really crude but also easy. – Pointy Nov 12 '10 at 23:35

7 Answers7

48

A simple solution is .split() and .pop() to get the last string in the array, like this:

var ext = fileName.split('.').pop();

This will get you just "txt" without the ., just append if needed. This also works on say: My.File.name.has.an extension.txt as well. If it doesn't have an extension it'll return the file name, so you may want to check for this...or go a completely different direction and validate against a set or known extensions via regex.

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
Nick Craver
  • 623,446
  • 136
  • 1,297
  • 1,155
  • this is what i am looking for thanks !!! – Gaurav_0093 Jun 07 '17 at 07:45
  • the script will not work correctly when having query-string parameters, e.g. `/example/image.jpg?a=b&b=2`, to handle it i added one more splits `file.split("?")[0].split('.').pop()` (note: a lot of times systems used query-string to prevent cache when needed) – Roy Shoa Nov 20 '17 at 10:28
9

Use lastIndexOf() and substr():

function getFileExtension(name)
{
    int found = name.lastIndexOf('.') + 1;
    return (found > 0 ? name.substr(found) : "");
}

Note that this implementation returns an empty string if the filename doesn't contain any period character (i.e. has no extension). Implementations based on split() sometimes return the full filename in that case.

Frédéric Hamidi
  • 258,201
  • 41
  • 486
  • 479
8

If you need to validate multiple extensions:

var filename = "picture.jpg";
var valid_extensions = /(\.jpg|\.jpeg|\.gif)$/i;   
if(valid_extensions.test(filename))
{ 
   alert('OK');
}
else
{
   alert('Invalid File');
}

This eliminates the need to parse the string if you want to check the extension, for example, before uploading a file.

Evan Mulawski
  • 54,662
  • 15
  • 117
  • 144
2
var text = 'example.txt',
    ext  = text.split('.')[1];
tbleckert
  • 3,763
  • 4
  • 32
  • 39
2
    if((document.form1.upload.value.lastIndexOf(".jpg")==-1) {
   alert("Please upload only .jpg extention file");
   return false;
}

lastIndexOf will return the index of the last occurrence of the specified search argument. If not found, -1 will return

Andrew Collins
  • 2,541
  • 3
  • 17
  • 16
1

Split the entire file name with . as the delimitter.The split will store the splitted items in an array.Take the last element of the array.

Shyju
  • 214,206
  • 104
  • 411
  • 497
0

Frédéric's answer worked well for me, however, the function keep erroring out in Chrome console with "Unexpected identifier". The "int" was issue and this modification worked.

function getFileExtension(name){
  var found = name.lastIndexOf('.') + 1;
  return (parseInt(found) > 0 ? name.substr(found) : "");
}
Minimul
  • 4,060
  • 2
  • 21
  • 18