Strip characters in file upload (Dreamweaver CS5) ASP/Jaavascript

Posted on 2011-10-05
Last Modified: 2012-05-12
I am using Classic ASP/Javascript and Dreamweave CS5. I have a page that uses a file field where you can browse for a file in your computer/network so that then it can be uploaded.
The problem I am having is with that some clients have their files named such as "Document - 1.doc".  Certain characters cause an error on upload, I am using an extension from DMXZone (Pure File Upload), it does  a great job renaming files and all but there is no function to strip the file name from certain characters to prevent this error. I want to prevent it at the time in which user browses for the file and submits the form, at that time I want to strip the file name from all this characters, like a comma or a '-' or even spaces cause errors.
How can this be done ?  I am attaching some of my code in case this is necessary.

---- This is the code for my form ---
<form action="<%=MM_editAction%>" method="POST" enctype="multipart/form-data" name="form1" onSubmit="checkFileUpload(this,'',true,'','','','','','','');showProgressWindow('blueFlashProgress.htm',300,200);return document.MM_returnValue">
              <table width="100%"  border="1" cellpadding="0" cellspacing="3" bordercolor="#ECE9D8">
                  <td colspan="2" class="bodytextTtileBlackBkgd"><div align="center">Add an Attachment </div></td>
                  <td width="15%" class="bodytextTtileBlack">File <span class="bodytext"><font color="#003366"><font color="#009900"><font color="#000000"><img src="../../Images/requiredstar.gif" width="9" height="8"></font></font></font></span></td>
                  <td width="85%"><input name="Filename" type="file" class="bodytext" id="Filename" onChange="checkOneFileUpload(this,'',true,'','','','','','','')" size="50"></td>
                  <td class="bodytextTtileBlack">Description <span class="bodytext"><font color="#003366"><font color="#009900"><font color="#000000"><img src="../../Images/requiredstar.gif" width="9" height="8"></font></font></font></span></td>
                  <td><input name="description" type="text" class="bodytext" id="description2" size="40"></td>
                  <td class="bodytextTtileBlack">Accessible by corporate client </td>
                  <td><span class="bodytext">
                    <input name="chkcorpclient" type="radio" value="1">
    <input name="chkcorpclient" type="radio" value="0" checked>
    No </span> </td>
                  <td class="bodytextTtileBlack">Accessible by individual client </td>
                  <td class="bodytext"><input name="chkindvclient" type="radio" value="1">
      <input name="chkindvclient" type="radio" value="0" checked>
    No </td>
                  <td colspan="2">                  <input name="imageField" type="image" onClick="WAValidateRQ(document.form1.description,'- Please enter the file description',document.form1.description,0,false,'text');WAAlertErrors('','',true,false);return document.MM_returnValue" src="../../Images/Buttons/BtAdd.gif" width="84" height="20" border="0">
                  <input type="hidden" name="MM_insert" value="form1">
                  <input name="UserId" type="hidden" id="UserId3" value="<%=(ContactDetails.Fields.Item("UserId").Value)%>">
                  <input name="FileNamenew" type="hidden" id="FileNamenew" value="Not yet ready"></td></tr>

Open in new window

Question by:amucinobluedot

    Author Comment

    Apparently this is the code that renames the file on upload, not sure if making a change in this code will prevent the error but might be worth the try. If somehow we can add code to strip the name from such characters might do the trick.

    // *** Rename Uploaded Files 1.7.0
    if (String(Request.QueryString("GP_upload")) != "undefined") {
      var REUF_path = '../../local/Attachments/'+Session('FirmId')
      var REUF_nameConflict = "over"
      var sTS=sTimeStamp;
      var REUF_renameMask = DoTrim(Session("FirmId"),2)+"_"+DoTrim(Request.Querystring("NewUserId"),2)+ "_"+sTS+"_"+"##name##"+".##ext##"
    <script language="vbscript" runat="server">

    Open in new window

    LVL 70

    Accepted Solution

    It's not so much what happens to the file after uploading as preventing files with the bad characters from uploading at all.  

    In other words, this is a client side validation issue and not a server-side issue.  Once it hits the server, it's too late.

    So think about javascript to check for the presence of an illegal character in the filename before the form is even submitted:

    <script language="javascript">
    function Checkfiles()
    var fup = document.getElementById('filename');
    var fileName = fup.value;
    var ext = fileName.substring(fileName.lastIndexOf('.') + 1);
    if(ext == "gif" || ext == "GIF" || ext == "JPEG" || ext == "jpeg" || ext == "jpg" || ext == "JPG" || ext == "doc")
    return true;
    alert("Upload Gif or Jpg images only");
    return false;

    Open in new window

    Code above shamelessly copied from

    The above checks the filename for the extension but it would be a snap to modify it to check for illegal characters and warn the user to rename the file before uploading it.

    Featured Post

    Training Course: Java/J2EE and SOA

    This course will cover both core and advanced Java concepts like Database connectivity, Threads, Exception Handling, Collections, JSP, Servlets, XMLHandling, and more. You'll also learn various Java frameworks like Hibernate and Spring.

    Join & Write a Comment

    Article by: DanRollins
    This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (, we'll extend the program by adding a depth-…
    Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
    The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
    The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

    731 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now