0 Replies Latest reply on Dec 15, 2010 11:23 AM by goirish34

    File upload - select existing image or upload a new image option when adding a new entry

    goirish34

      Any  ideas on how I can build a function that allows me to select an  existing image stored in a folder to update an entry on a database  driven page?  Below is an edit page and the related update page.  See  how I handle the image being pulled out of the database.  This works fine when I am changing the page and either uploading a new image or not changing the image.  Whenever I add  an entry or edit an entry, I would like to be able to either upload a  new image, or grab an existing image in a specified folder.  Thoughts?

       

      Page 1

       

      <!--- Entry form --->

      <br>

      Thumbnail images should be sized at 140 x 140 pixels at 72 dpi<br>

      Full size images should be sized at 475 pixels wide x 200-250 pixels height at 72 dpi

      <br>

      <br>

      <cfoutput query="getinfo">

      <CFFORM action="editpost2.cfm" name="miscellaneous" method="post" enctype="multipart/form-data">

      <input type="hidden" name="ID" value="#ID#">

      <TABLE>

       

       

          <TR>

          <TD valign="top"> Post Department </TD>

          <TD>

       

      <select name="Department_ID">

      <cfoutput>

      <cfloop query="getdepartments2"><option value="#ID#">#Title#</option></cfloop>

      <cfloop query="getdepartments"><option value="#ID#">#Title#</option></cfloop>

      </cfoutput>

      </select>

       

                     </TD>

                     </TR>

       

              <TR>

          <TD valign="top"> Post Categories </TD>

          <TD>

       

      <cfoutput>

      <cfloop  query="getstuff"><input name="Category_ID" type="checkbox"  value="#getstuff.B#" <cfif  ListFind(roleList,getstuff.B)>checked</cfif>>#getstuff.D#<br></cfloop>

      </cfoutput>

       

                     </TD>

                     </TR

       

          ><TR>

          <TD valign="top"> Title </TD>

          <TD>

       

                                    <INPUT type="text" name="title" value="#Title#" size="60" maxLength="100">

       

                     </TD>

                     </TR>

       

          <TR>

          <TD valign="top"> Subtitle </TD>

          <TD>

       

                                    <INPUT type="text" name="Subtitle" value="#Subtitle#" size="60" maxLength="100">

       

                     </TD>

                     </TR>

       

       

      <tr>

      <td valign="top">Thumbnail</td>

      <td valign="top">

      <input type="file" name="Image" value="" size="50">

      <input type="hidden" name="oldimage" value="#getinfo.Image#">

      <cfif getinfo.image is not "">

      <br><b><a  href="../sharedimages/#getinfo.Image#" target="_blank"  style="text-decoration: underline; color: ##63bded; font-weight:  600;">[View #getinfo.Image#]</a></b><br><br>

      </cfif>

      </td>

      </tr>    

       

          <TR>

          <TD valign="top"> Photo Credit </TD>

          <TD>

       

                                    <INPUT type="text" name="PhotoCredit" value="#PhotoCredit#" size="60" maxLength="100">

       

                     </TD>

                     </TR>  

       

      <tr>

      <td valign="top">Image</td>

      <td valign="top">

      <input type="file" name="Image2" value="" size="50">

      <input type="hidden" name="oldimage2" value="#getinfo.Image2#">

      <cfif getinfo.image2 is not "">

      <br><b><a  href="../sharedimages/#getinfo.Image2#" target="_blank"  style="text-decoration: underline; color: ##63bded; font-weight:  600;">[View #getinfo.Image2#]</a></b><br><br>

      </cfif>

      </td>

      </tr>    

       

          <TR>

          <TD valign="top"> Photo Credit2 </TD>

          <TD>

       

                                    <INPUT type="text" name="PhotoCredit2" value="#PhotoCredit2#" size="60" maxLength="100">

       

                     </TD>

                     </TR>                 

       

          <TR>

          <TD valign="top"> Description </TD>

          <TD>

                      <cftextarea width="800" height="250" name="Description"  richtext="yes" enabled="yes">#Description#</cftextarea>

       

                     </TD>

                     </TR>  

       

          <TR>

          <TD valign="top"> Description2 </TD>

          <TD>

                      <cftextarea width="800" height="250" name="Description2"  richtext="yes" enabled="yes">#Description2#</cftextarea>

       

                     </TD>

                     </TR>

       

       

                     <TR>

          <TD valign="top"> Download Link </TD>

          <TD>

       

                                     <INPUT type="text" name="DownloadLink" value="#DownloadLink#"  size="40" maxLength="100"><br> (example:  http://www.cnn.com/test.pdf)

       

                     </TD>

                     </TR>

       

       

      <tr>

      <td>Active Date</td>

      <td><cfinput type="datefield" name="Active_DT" value="#DateFormat(Active_DT,"mm/dd/yyyy")#"></td>

      </tr>

      <tr>

      <td>Expiration Date</td>

      <td><cfinput type="datefield" name="Expiration_DT" value="#DateFormat(Expiration_DT,"mm/dd/yyyy")#"></td>

      </tr> 

       

          <TR>

          <TD valign="top"> Active </TD>

          <TD>

       

                                                                  <select name="Active">

                     <option value="#Active#">#Active#</option>                                   

                     <option value="Yes">Yes</option>

                     <option value="No">No</option>

      </select>

       

                     </TD>

                     </TR>

       

          <TR>

          <TD valign="top"> Approved </TD>

          <TD>

       

                                                                  <select name="Approved">

                     <option value="#Approved#">#Approved#</option>                                      

                     <option value="Yes">Yes</option>

                     <option value="No">No</option>

      </select>

       

                     </TD>

                     </TR>  

       

                     <TR>

          <TD valign="top"> Display Order </TD>

          <TD>

       

                                    <INPUT type="text" name="DisplayOrder" value="#DisplayOrder#" size="5" maxLength="5">

       

                     </TD>

                     </TR>                 

       

       

      </TABLE>

      <P>

      <input type="submit" value="Edit Post">

      </CFFORM>

       

      Page 2

       

      <cfinclude template="adminsecurityheader.cfm">

       

      <cfif FORM.Image neq "">

      <!--- first actually upload the file --->

      <cffile ACTION="UPLOAD"

              FILEFIELD="Image"

              DESTINATION="#ImagesUpload#"

                                    NAMECONFLICT="OVERWRITE">                                                          

      <cfset Image = #cffile.serverFileName# & "." & #cffile.serverFileExt#>                     

       

      <!--- delete old file if the name is not the same --->                          

      <cfif #oldimage# NEQ "">

      <cfif #Image# NEQ #oldimage#>

      <cfif FileExists("#ImagesUpload#\#oldimage#")>

      <cffile ACTION="DELETE" FILE="#ImagesUpload#\#oldimage#">

      </cfif>

      </cfif>

      </cfif>

       

      <cfelse>

      <cfset Image = "#oldimage#">                                 

      </cfif>

       

       

      <cfif FORM.Image2 neq "">

      <!--- first actually upload the file --->

      <cffile ACTION="UPLOAD"

              FILEFIELD="Image2"

              DESTINATION="#ImagesUpload#"

                                    NAMECONFLICT="OVERWRITE">                                                          

      <cfset Image2 = #cffile.serverFileName# & "." & #cffile.serverFileExt#>                   

       

      <!--- delete old file if the name is not the same --->                          

      <cfif #oldimage2# NEQ "">

      <cfif #Image2# NEQ #oldimage2#>

      <cfif FileExists("#ImagesUpload#\#oldimage2#")>

      <cffile ACTION="DELETE" FILE="#ImagesUpload#\#oldimage2#">

      </cfif>

      </cfif>

      </cfif>

       

      <cfelse>

      <cfset Image2 = "#oldimage2#">                                           

      </cfif>

       

       

      <cfparam name="Category_ID" default="0">

       

      <cflock timeout="20" throwontimeout="No" name="#CreateUUID()#">

      <cftransaction>

       

      <cfquery  name="getinfo" datasource="#Application.DS#"  username="#Application.DSusername#" password="#Application.DSpassword#"  dbtype="ODBC">

      UPDATE Posts SET

      Department_ID = <cfqueryparam cfsqltype="CF_SQL_INTEGER" maxlength="5" value="#Department_ID#">,

      Title = <cfqueryparam cfsqltype="CF_SQL_VARCHAR" maxlength="150" value="#Title#">,

      Subtitle = <cfqueryparam cfsqltype="CF_SQL_VARCHAR" maxlength="150" value="#Subtitle#">,

      Image = <cfqueryparam cfsqltype="CF_SQL_VARCHAR" maxlength="150" value="#Image#">,

      PhotoCredit = <cfqueryparam cfsqltype="CF_SQL_VARCHAR" maxlength="150" value="#PhotoCredit#">,

      Image2 = <cfqueryparam cfsqltype="CF_SQL_VARCHAR" maxlength="150" value="#Image2#">,

      PhotoCredit2 = <cfqueryparam cfsqltype="CF_SQL_VARCHAR" maxlength="150" value="#PhotoCredit2#">,

      Description = <cfqueryparam cfsqltype="CF_SQL_LONGVARCHAR" value="#Description#">,

      Description2 = <cfqueryparam cfsqltype="CF_SQL_LONGVARCHAR" value="#Description2#">,

      DownloadLink = <cfqueryparam cfsqltype="CF_SQL_VARCHAR" maxlength="150" value="#DownloadLink#">,

      Active = <cfqueryparam cfsqltype="CF_SQL_VARCHAR" maxlength="10" value="#Active#">,

      Approved = <cfqueryparam cfsqltype="CF_SQL_VARCHAR" maxlength="10" value="#Approved#">,

      DisplayOrder = <cfqueryparam cfsqltype="CF_SQL_VARCHAR" maxlength="100" value="#DisplayOrder#">,

      Active_DT = <cfqueryparam cfsqltype="CF_SQL_VARCHAR" maxlength="100" value="#Active_DT#">,

      Expiration_DT = <cfqueryparam cfsqltype="CF_SQL_VARCHAR" maxlength="100" value="#Expiration_DT#">

      WHERE ID = <cfqueryparam cfsqltype="CF_SQL_INTEGER" maxlength="5" value="#ID#">

      </cfquery>

       

      <cfquery  name="del_roles" datasource="#Application.DS#"  username="#Application.DSusername#" password="#Application.DSpassword#"  dbtype="ODBC">

                     delete from PostCats where Post_ID = <cfqueryparam cfsqltype="CF_SQL_INTEGER" maxlength="5" value="#ID#">

      </cfquery>

       

      <!--- now lets insert the roles --->

      <cfloop list="#Category_ID#" index="idx">

                      <cfquery name="ins_roles" datasource="#Application.DS#"  username="#Application.DSusername#" password="#Application.DSpassword#"  dbtype="ODBC">

                                    insert into PostCats

                                                   (Post_ID, Category_ID)

                                    values

                                                    (<cfqueryparam cfsqltype="CF_SQL_INTEGER" maxlength="5"  value="#ID#">,#val(idx)#)

                     </cfquery>

      </cfloop>

      </cftransaction>

      </cflock>