7 Replies Latest reply on May 23, 2006 6:38 AM by brianism

    cffile - upload default file

    splitzer Level 1

      Hello,
      I've a upload form, that uplaod an image than insert into db along with text fields. The problem is, when i dont upload a file it gives me an error that i MUST upload a file.

      How can i make it not show this message and insert the default file name like noimage.gif to the db?

      Here is the code:

      <cffile action="upload" filefield="Image1" destination="E:\Domains\domain.info\wwwroot\admin\upload\" nameconflict="MakeUnique" accept="image/jpeg, image/pjpeg, image/gif, image/png, image/x-png" >
      <p align="center">Click the confirm button to insert into the database</p>

      .......................................................................................... .....................................................

      <cfelseif isdefined("form.image1") and Len(Trim(form.image1)) EQ 0>
      <p>You must enter the image to enter image </p>
      </cfif>



      This code suppose to uplaod image, than tell whether it was successful or say You must enter the image to enter image

      Thanks in advance!
        • 1. Re: cffile - upload default file
          brianism Level 1
          Try this out:


          <cfif isdefined("form.image1")>

          <cfif #form.image1# is "">
          <cfset imgFilename1="noimage.gif">
          <cfelse>
          <CFFILE destination="E:\Domains\domain.info\wwwroot\admin\upload\"
          action="UPLOAD"
          nameconflict="makeunique"
          filefield="exttb"
          accept="image/*"
          mode="644">
          <cfset imgFilename1="#file.ClientFile#">
          </cfif>

          </cfif>


          <!---insert into the database--->

          <cfquery datasource="yourdbname">
          INSERT INTO yourtablename
          SET
          Image1 = '#imgFilename1#',
          text1 = '#form.text1#'
          </cfquery>
          • 2. Re: cffile - upload default file
            splitzer Level 1
            It gave me this error:
            Element SERVERFILE is undefined in CFFILE.

            The element is being used on these lines:
            <td><cfoutput><img src="upload/#cffile.serverfile#"></cfoutput></td>
            </tr>
            <tr valign="top">
            <td></td>
            <td><cfoutput>#cffile.ServerFile#</cfoutput>
            • 3. Re: cffile - upload default file
              primalx2003 Level 1
              Try this:
              <cfif isdefined("PhotoTitle") and isdefined("Photo")>
              <cfset curDirectory = "c:\Photos\">
              <cfif isdefined("Add") and Add is "yes">
              <cfset maximumFileSize = "50000">
              <CFIF Val(CGI.CONTENT_LENGTH) GT maximumFileSize>
              The file you are trying to upload exceeds the maximum allowable size of <CFOUTPUT><B>#maximumFileSize#</B></CFOUTPUT> (50K) bytes and cannot be uploaded to this server! Please choose a smaller image and try again. Click here to <A HREF="javascript nclick=window.history.go(-1);">return.</A>
              <CFABORT>
              </CFIF>

              <cfif ISDEFINED("Photo") and Photo NEQ ''>
              <CFFILE
              ACTION="upload"
              FILEFIELD="Photo"
              DESTINATION="#curDirectory#"
              NAMECONFLICT="MAKEUNIQUE"
              ACCEPT="image/*"
              >
              <cfset Photo = #file.serverfile#>
              </cfif>
              <cfquery datasource="#application.datasource#" name="whatever">
              insert into Photos(Phototitle,Photo)
              Values ('#Phototitle#'',<cfif ISDEFINED("Photo") and Photo NEQ ''>'#Photo#'<cfelse>noimage.gif</cfif>
              </cfquery>
              </cfif>

              If the image field is required you can use javascripts to make sure they do enter an image.

              <script language="JavaScript">
              <!--
              <!-- Hide the code from old stupid Browsers
              window.onerror=null; //Error handler
              function checkit(photos) {
              var primary = ""

              if (!document.photos.PhotoTitle.value)
              { primary = primary + " Photo Title is needed\n" }

              if (!document.photos.Photo.value)
              { primary = primary + " Photo is needed\n" }

              //After the commend goes through the checking...
              if (primary == "") {
              document.photos.submit();
              }

              //If the condition is not met, this happens..
              else
              {
              alert ("Please Fillout:\n" + primary + "!!")
              return false;
              }
              }
              // -->
              </script>

              <form id="photos" method="post" action="whereever.cfm" NAME="photos" OnSubmit="return checkit(); return false;" ENCTYPE="multipart/form-data">
              <table width="392" height="77" border="0" align="left" cellpadding="1" cellspacing="1" bgcolor="#000000">
              <tr>
              <td valign="top" bgcolor="#FDC91D">Title:</td>
              <td valign="top" bgcolor="#202020"><input name="PhotoTitle" type="text" value="" size="35" /></td>
              </tr>
              <tr>
              <td valign="top" bgcolor="#FDC91D">Photo:</td>
              <td valign="top" bgcolor="#202020"><input name="Photo" type="file" id="Photo" value="" size="35" /></td>
              </tr>
              <tr>
              <td valign="top" bgcolor="#202020"><div align="right">
              <input name="Submit3" type="submit" value="Submit" />
              </div></td>
              </tr>
              </table>
              </form>


              Try it; I hope it works for you.
              • 4. Re: cffile - upload default file
                brianism Level 1
                In regards to the error:

                Element SERVERFILE is undefined in CFFILE.

                Try using #file.serverfile# instead of #cffile.serverfile# in the output.
                • 5. Re: cffile - upload default file
                  brianism Level 1
                  Acutally,
                  I just thought of something...you may be getting that error if not file is uploaded. Try this output code:


                  <td>
                  <cfif imgFilename1 NEQ "">
                  <cfoutput><img src="upload/#file.serverfile#"></cfoutput>
                  </cfif>
                  </td>
                  </tr>
                  <tr valign="top">
                  <td></td>
                  <td>
                  <cfif imgFilename1 NEQ "">
                  <cfoutput>#file.ServerFile#</cfoutput>
                  </cfif>
                  • 6. Re: cffile - upload default file
                    splitzer Level 1
                    I tried inserting your code like this:
                    <cfelseif isdefined("form.image1") and Len(Trim(form.image1)) EQ 0>
                    <p>You must enter the image to enter property. </p>

                    <cfif image1 NEQ "">
                    <cfoutput><img src="upload/#file.serverfile#"></cfoutput>
                    </cfif>
                    </td>
                    </tr>
                    <tr valign="top">
                    <td></td>
                    <td>
                    <cfif image1 NEQ "">
                    <cfoutput>#file.ServerFile#</cfoutput>
                    </cfif>

                    </cfif>



                    And it gave me this error:
                    Element SERVERFILE is undefined in CFFILE.
                    • 7. Re: cffile - upload default file
                      brianism Level 1
                      Do you have the enctype in the form on the first page like this?

                      <form method="post" action="" ENCTYPE="multipart/form-data">

                      If it is not multipart/form-data it will not recognize the image1 formfield.