4 Replies Latest reply on Sep 6, 2007 12:47 PM by shearak

    File attachment validation

    shearak
      So I have a form that allows users to attach documents by browsing for the files. How would I check if a files exists where the user doesn't use browse to attach a file but rather types in a file and path name (and the file isn't found)?


      <form action="Contact_Submission_Action.cfm" method="post" enctype="multipart/form-data" name="contact_submission" id="contact_submission" onSubmit="MM_validateForm('Email','','RisEmail');return document.MM_returnValue">
      <table width="815" border="0" align="center" class="submission_form">
      <tr>
      <td colspan="10"> </td>
      </tr>
      <tr>
      <td colspan="2" class="fieldheaderleft">Your e-mail address:<br><span class="note">(required) </span></td>
      <td colspan="8"><input type="text" name="Email" size="50" value=""></td>
      </tr>
      <tr>
      <td colspan="2" class="fieldheaderleft">Date to publish this item: <span class="note">(required) </span></td>
      <td colspan="8"><input type="text" name="date" value=" mm/dd/yy"></td>
      </tr>
      <tr>
      <td colspan="2" class="fieldheaderleft">Headline:</td>
      <td colspan="8"><input type="text" name="Headline" size="66" value=""></td>
      </tr>
      <tr>
      <td colspan="2" class="fieldheaderleft">Attachment #1:</td>
      <td colspan="8"><input type="file" name="attachment_1" size="50" value=""></td>
      </tr>
      <tr>
      <td colspan="2" class="fieldheaderleft">Attachment #2:</td>
      <td colspan="8"><input type="file" name="attachment_2" size="50" value=""></td>
      </tr>
      <tr>
      <td colspan="2" class="fieldheaderleft">Attachment #3:</td>
      <td colspan="8"><input type="file" name="attachment_3" size="50" value=""></td>
      </tr>
      <tr>
      <td colspan="2" class="fieldheaderleft">Description of related item (text for the link):</td>
      <td colspan="8"><textarea name="WhatAdditional" cols="50" rows="2" id="WhatAdditional"></textarea></td>
      </tr>
      <tr>
      <td colspan="2" class="fieldheaderleft">Comments or additional instructions for this item:</td>
      <td colspan="8"><textarea name="Comments" cols="50" rows="6" wrap="VIRTUAL" id="Headline"></textarea></td>
      </tr>
      <tr>
      <td colspan="2"> </td>
      <td colspan="8"><input type="submit" name="Send_ Email" value="Submit News Item"><input name="Clear form" type="reset" value="Clear Form"></td>
      </tr>
      </table>
      </form>

      Thanks in advance,
        • 1. File attachment validation
          Grizzly9279 Level 1
          I think the best you can do is attempt to upload the file (using <cffile action="upload">). If the file did not exist on the client machine, <cffile> should throw an error which says something like:

          "No data was received in the uploaded file "foo.txt".

          You can wrap some try/catch logic around the <cffile action="upload"> tag so you can detect this condition and treat it as you best see fit.
          • 2. Re: File attachment validation
            Level 7
            Have you tested if this even gets past the browser? Since it has to get
            the file and put it in the header of the request to the server, I would
            presume that if the browser can not find the file it is going to fail at
            that point. But I have never tested this so I do not know what the
            common behavior of browsers would be for uploading a non-existing file.
            • 3. Re: File attachment validation
              shearak Level 1
              Here is my action page using <cffile action="upload">. The upload works fine if the user browes and attaches the files. But if the user types in a file and path name (and the file isn't found) I get an error. Can someone show me how I can validate if the file/s do not exist.

              <cfset dir = ExpandPath("#request.app.Attach#/")>
              <cfif FORM.attachment_1 neq "">
              <!--- first actually upload the file --->
              <cffile action="upload"
              filefield="attachment_1"
              destination="#dir#/"
              nameconflict="Makeunique">
              <!--- now create a temporary holder for the attachment later on --->
              <cfset attachment_local_file_1 = "#dir#/#file.serverfile#">
              </cfif>
              <cfif FORM.attachment_2 neq "">
              <!--- first actually upload the file --->
              <cffile action="upload"
              filefield="attachment_2"
              destination="#dir#/"
              nameconflict="Makeunique">
              <!--- now create a temporary holder for the attachment later on --->
              <cfset attachment_local_file_2 = "#dir#/#file.serverfile#">
              </cfif>
              <cfif FORM.attachment_3 neq "">
              <!--- first actually upload the file --->
              <cffile action="upload"
              filefield="attachment_3"
              destination="#dir#/"
              nameconflict="Makeunique">
              <!--- now create a temporary holder for the attachment later on --->
              <cfset attachment_local_file_3 = "#dir#/#file.serverfile#">
              </cfif>

              Thank you for your submission! Your news item has been sent to . A member of the group will contact you with any questions or to review your item.

              <cfmail from="#form.email#" to="" subject="BS&R News Item Submission Form">
              E-Mail: #Email#
              News Section: #News#
              Date to publish this item: #date#
              Headline: #headline#
              News item text: #NewsItem#

              Attachment 1: #A1#
              Description 1: #Description1#
              Attachment 2: #A2#
              Description 2: #Description2#
              Attachment 3: #A3#
              Description 3: #Description3#


              Link to related information (if any): #RelateLink#
              What is this link? #WhatLink#
              Additional link to related information (if any): #AdditionalLink#
              What is this additional link? #WhatAdditional#

              Comments or additional instructions for this item:
              -------------------------------------------------------
              #Comments#

              -------------------------------------------------------

              <cfsilent>
              <cfif FORM.attachment_1 neq "">
              <cfmailparam file="#attachment_local_file_1#">
              </cfif>
              <cfif FORM.attachment_2 neq "">
              <cfmailparam file="#attachment_local_file_2#">
              </cfif>
              <cfif FORM.attachment_3 neq "">
              <cfmailparam file="#attachment_local_file_3#">
              </cfif>
              </cfsilent>
              </cfmail>

              Thanks again!
              • 4. Re: File attachment validation
                shearak Level 1
                Dump! Any suggestions please.