[Webinar] Streamline your web hosting managementRegister Today

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 370
  • Last Modified:

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

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

1 Solution
AleksAuthor Commented:
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

Jason C. LevineNo oneCommented:
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 http://www.dynamicdrive.com/forums/showthread.php?t=37439

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

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now