473,396 Members | 1,693 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 473,396 developers and data experts.

how to get file extension using javascript

sandeep thakur
Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3. <script type="text/javascript" language="javascript">
  4. function valid(ele)
  5. {
  6.   var file_name= ele.file.value;
  7.  
  8.  
  9.   var file_array = file_name.split(".");
  10.  
  11.   var file_array1 = file_array[1].toLowerCase();   
  12.  
  13.  
  14.   if (file_array1 == 'gif' || file_array1 == 'jpg' || file_array1 == 'jpeg',file_array1=='gif')
  15.   {
  16.     return true;  
  17.   }
  18.   else {
  19.  
  20.        alert("Please Upload Valid Image File");
  21.        ele.file.focus();
  22.        return false;
  23.   }      
  24.  
  25. }
  26. </script>
  27.  
  28. </head>
  29. <body>
  30.  
  31. <form name="form1" action="upload_file.php" method="post" enctype="multipart/form-data" onsubmit="return valid(this);">
  32. <label for="file">Filename:</label>
  33. <input type="file" name="file" id="file" /> 
  34. <br />
  35. <input type="submit" name="submit" value="Submit" />
  36. </form>
  37.  
  38. </body>
  39. </html>
Dec 11 '10 #1
1 9045
gits
5,390 Expert Mod 4TB
line 14 is quite weird ... the following part:
Expand|Select|Wrap|Line Numbers
  1. file_array1 == 'jpeg',file_array1=='gif'
will avoid to match 'jpeg' since the first 2 conditions are false and the comma-operator will force the execution of the 'gif' check again at last and so 'jpeg' will never match. just drop everything after the comma.

it could be simply improved to add more filetypes when using a filetype-map like:
Expand|Select|Wrap|Line Numbers
  1. var ft = { 'gif': 1, 'jpg': 1, 'png': 1 };
  2.  
  3. // now use a simple existence-check like:
  4. if (splittedFileType in ft) {
  5.     // valid
  6. } else {
  7.     // invalid
  8. }
  9.  
Dec 12 '10 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Sergio Otoya | last post by:
Hi all, Is there any way of copying a file using javascript, not using the Filesystemobject (ActiveX). I need this to run in Windows and MACS. Any help would be greatly appreciated. Thanks...
6
by: monki_sf | last post by:
Was wondering if anyone knew of way to validate an images file size using javascript - i.e keeping the upload to say 500k. I've seen scripts to validate the dimensions - but nothing for the actual...
1
by: guitarsajee | last post by:
hi.. i want to know how to validate file tag using javascript... plz let me know soon as possible.. thanx...
4
by: gsuns82 | last post by:
hi friends,can any one give the coding for finding file size using java script???
9
by: Mahernoz | last post by:
Hello Friends, The JavaScript File exmplmenu_var.js contains the code... (for the sake of brevity i am showing only that code which needs to be changed) I am actually developing a menu using...
1
by: seejay1982 | last post by:
Hi, I am writing an application where I create a temporary file on the client machine and I have to upload this file to a CGI on the server. This needs to be done without the user interaction. ...
13
by: marfola | last post by:
I’m trying to find the file size using javascript, before it get uploaded into the server using the following Javascript (I don’t want to use ActiveX because of compatibility problem). function...
6
by: mitchell | last post by:
hi i was trying to delete a file in a javascript function but couldn't do it. myActiveXObject = new ActiveXObject("Scripting.FileSystemObject"); file =...
3
Frinavale
by: Frinavale | last post by:
Hi there! I'm hoping that someone knows how to check the size of a file before it is uploaded to the server using JavaScript. I have seen suggested solutions using an ActiveX control to check...
4
by: govind161986 | last post by:
How do I validate a file size using javascript? Thanks in advance, Govind
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.