0

for the

<input type="file" name="addfile" id="addfile" />
 <input type="submit" id="btnadd" value="Addfile" onclick="filetype(addfile)" />

i need to check the file type(pdf or doc etc) in javascript that is added for this what do i need to pass in filetype onclick function to have addfile name passed instead of addfile passed in it ?

thanks,

michaeld

michael
  • 575
  • 2
  • 14
  • 27

1 Answers1

0

Well first you need to extract the filename from addfile after its added, like as mentioned here: Javascript - How to extract filename from a file input control

and then use that inside filetype.

Community
  • 1
  • 1
Mrchief
  • 75,126
  • 20
  • 142
  • 189
  • hi Mrchief the code gives me file name like new.txt, file.xml , what to add to have only extension like .txt, .xml – michael Jul 22 '11 at 14:29
  • Try this: `var ext = filename.split('.')[1]; // filename.split('.')[0] will be jsut the name` – Mrchief Jul 22 '11 at 14:32
  • i tried like this while (fullPath.indexOf("\\") != -1) fullPath = fullPath.slice(file.indexOf("\\") + 1); ext = fullPath.slice(fullPath.indexOf(".")).toLowerCase(); it also wrkd ,thanks mrchief – michael Jul 22 '11 at 14:38
  • Yeah, that'd work too. I just prefer `split` as it is shorter and cleaner and doesn't make calculate those pesky indexes. – Mrchief Jul 22 '11 at 14:43