[thelist] MIME type accessible to javascript?

.jeff jeff at members.evolt.org
Thu Mar 28 15:32:01 CST 2002


judah,

><><><><><><><><><><><><><><><><><><><><><><><><><><><><><
> From: Judah McAuley
>
> Dillema: In a form, we only want to allow certain file
> types to uploadable.  We can check the MIME type once it
> is uploaded to the server, but that's not a very
> appealing option.  It would certainly be preferable to
> check the MIME type when the person selects the file to
> upload.  Is this possible via javascript and if so, how
> do you do it?
><><><><><><><><><><><><><><><><><><><><><><><><><><><><><

the only way you can do it is to check the file extension.  for that you'll need one of my cftojs list functions, ListFind() or ListFindNoCase(), depending on whether you want to enforce case-sensitivity in the file naming:

function ListFind(list, value)
{
  var i = 0;
  var delimiter = ',';
  var returnValue = -1;
  var _tempArray = new Array();
  if(ListFind.arguments.length == 3)
    delimiter = ListFind.arguments[2];
  _tempArray = list.split(delimiter);
  for(i = 0; i < _tempArray.length; i++)
  {
    if(_tempArray[i] == value)
    {
      returnValue = i;
      break;
    }
  }
  return returnValue;
}

function ListFindNoCase(list, value)
{
  var i = 0;
  var delimiter = ',';
  var returnValue = -1;
  var _tempArray = new Array();
  if(ListFindNoCase.arguments.length == 3)
    delimiter = ListFindNoCase.arguments[2].toLowerCase();
  list = list.toLowerCase();
  value = value.toLowerCase();
  _tempArray = list.split(delimiter);
  for(i = 0; i < _tempArray.length; i++)
  {
    if(_tempArray[i] == value)
    {
      returnValue = i;
      break;
    }
  }
  return returnValue;
}

you'll also need another of my cftojs list functions, ListLast(), to get the file extension from the entire path.

function ListLast(list)
{
  var delimiter = ',';
  var returnValue = '';
  var _tempArray = new Array();
  if(ListLast.arguments.length == 2) delimiter = ListLast.arguments[1].toLowerCase();
  _tempArray = list.split(delimiter);
  if(_tempArray.length)
    returnValue = _tempArray[_tempArray.length - 1];
  else
    returnValue = list;
  return returnValue;
}

now, just find it like you would with coldfusion (use ListFindNoCase() instead of ListFind() in the example below if you don't care about the case of the file extension):

if(ListFind('gif,jpg,jpeg,bmp,psd,png,tif,pdf', ListLast(form.fileinput.value, '.') == -1)
{
  // file doesn't have one of the accepted extensions.
}
else
{
  // file has one of the accepted extensions.
}

good luck,

.jeff

http://evolt.org/
jeff at members.evolt.org
http://members.evolt.org/jeff/




More information about the thelist mailing list