logo

New Response

« Return to the main article

You are replying to:

    • avatar
    • Justen
    • Posted on Sun 22 Feb 2009

    function TestFileType( fileName, fileTypes ) {

    if (!fileName) return;

    dots = fileName.split(".");

    //get the part AFTER the LAST period.

    fileType = "." + dots[dots.length-1];

    return ('.'+fileTypes.join(".").indexOf(fileType) != -1) ?

    alert('That file is OK!') :

    alert("Please only upload files that end in types: \n\n ." + (fileTypes.join(" .")) + "\n\nPlease select a new file and try again.");

    Missing a semicolon at dots = fileName..., I know that's not a big deal, also included the initial '.' before the fileTypes.join and in the alert box for cosmetic purposes. Otherwise the first file type in the file array sometimes gets ignored.

    Tangentially, if you want to be able to clear the file input box: IE won't let you change the value of an input type=file box directly, so you need to delete the input element and replace it with a fresh new element. If you want it to trigger automatically, bind it to the 'change' event, either by using the inline onchange='' attribute or using your event binding method of choice.

Your Comments

Name:
E-mail:
(optional)
Website:
(optional)
Comment: