< Back

Document Subject: Bad characters in Web File Upload causes attachments to fail
Hint Short Cut: Add this to your code & documentation to help you find this page.
http://#BadCharactersInUploadedFiles or http://A555F9/nn.nsf/ByAlias/BadCharactersInUploadedFiles

When uploading files that have filenames with %, spaces, plusses, quotes, ampersands etc in them, cause the web page to not link correctly to the attachment, causing a 'This page cannot be found' error message. Here's how to stop it.




Create a File upload control as normal.

Save button should be something like:
<A HREF="javascript:checkFields()"><IMG SRC="save.gif" BORDER="0" ALT="Save"></a>

then in form's JS Header:

 

function checkFields()
{
var f = document.forms[0];
if ( f.Project.selectedIndex == 0  )  
               {
      alert ("You must select a Project")
 f.Project.focus();
}  
else if ( f.Title.value == "" )  
              {
      alert ("You must fill in a title")
 f.Title.focus();
}  
else
              {
 if ( getFileNames() )        
                              {  // passed validation
                                f.submit();
                               }
}
} // end of checkFields

function getFileNames()  
               {
var f = document.forms[0];
var allelements = f.elements.length;
for (i=0; i<allelements; i++)
                      {
 var doctype = f.elements[i].type;
 var docval = f.elements[i].value;
 if(doctype == "file")
                               {
  var start = docval.lastIndexOf("\\") + 1;
  var end = docval.length;
  var filename = docval.slice(start,end);
 
 // check filenames do not contain spaces, plusses or $ or & ' or " etc
                var BadChars = " !"+'"'+"£$%^&*()+'/\?,@`|¬"

                 for (c = 0; c < BadChars.length; c++)
                                              {
                       if (filename.indexOf(BadChars.substring(c,c+1)) !=-1  )
                                                      {
                       alert ("Found '"+BadChars.substring(c,c+1)+"' in\n "+ filename +"\n\n( space "+BadChars+" not allowed! )");
                       f.elements[i].focus();
                      return false;
                          }  // if bad char
  }  // end for badchars      
        } // element is of type file
 } // go through all elements
return true;
} //end of check files