Discussion Area

ask questions, discuss topics, solve problems

This is a public Discussion Area  publicRSS

Thread

    • CFFILE-UPLOAD in a Function?
      Thread posted Mar 16 by BKinMV
      598 Views, 6 Comments
      Title:
      CFFILE-UPLOAD in a Function?
      Content:

      Is there anyway to use CFFILE with ACTION="upload" in a function, i.e., have variable FILEFIELD values? Documents say "Do not use number signs (#) to specify the field name."

      I have multiple <CFINPUT TYPE="FILE">s on a page and would like to minimize code.

    Comments

    • I guess we can esily use <cfile action="upload" filefield="xxxx" destination="destination"> easily in functions also.

      What problem are you facing to get it done?

      • I need the xxxx in filefield="xxxx" to be a variable so it can be used with multiple input fields. However, filefield="#FileContents#" returns an error (and is prohibited by the documentation). So does:

        <cfset xxxx = FileContents><!---or #FileContents# or "#FileContents#"--->

        <cfile action="upload" filefield="xxxx" destination="destination">

        Is there some other way? Thanks.

         

    • Assuming "FileContents" is filefield on the form:

      Use:

      <cfset xxxx = "FileContents">

      <cfile action="upload" filefield="#xxxx#" destination="destination">

      This should work.

      • Yes it does! Thank you very much.

    • Good to know, it solved your problem.

    • Here's a function I've created in the past to handle multiple file uploads. It also inserts some data into a table, so I take two arguements, the form field name and the item_id to attach the picture to. This function also has a list of acceptable file formats to prevent invalid file types from being uploaded. Finally, I also resize the images and take care of that after the upload at the same time. Hopefully this might be of some help to others.

      <CFFUNCTION NAME="image_upload" ACCESS="private">
         
          <CFARGUMENT NAME="itemId" TYPE="numeric" REQUIRED="Yes">
          <CFARGUMENT NAME="field"  TYPE="string"  REQUIRED="Yes">
         
          <!--- intialize variables --->
          <CFSET var DoCommit      = "Yes">
          <CFSET var itemResults   = "">
          <CFSET var insertResults = "">
          <CFSET var imageDir      = "D:\Servers\Inetpub\Domains\somewebsite.com\images\horses">
          <CFSET var imageResult   = "">
          <CFSET var formatAccept  = "jpg,png,gif,jpeg,pjepg">
          <CFSET var myImage       = "">
          <CFSET var imageSizes    = "225|150,68|68"><!--- this list must be parallel with imageFolders --->
          <CFSET var imageFolders  = "medium,small"><!--- this list must be parallel with imageSizes --->
          <CFSET var index         = "">
         
          <CFTRY>
              <!--- upload image --->
              <CFFILE ACTION="upload"
                  FILEFIELD="#ARGUMENTS.field#"
                      DESTINATION="#imageDir#\original\"
                          NAMECONFLICT="MakeUnique"
                              RESULT="imageResult">
                             
              <!--- make sure it's a valid image format, if not, then delete it --->
              <CFIF ListFind(formatAccept, LCase(imageResult.serverFileExt)) EQ 0>
                  <CFFILE ACTION="delete"
                      FILE="#imageDir#\original\#imageResult.serverFile#">
                  <CFSET DoCommit = "No">   
              </CFIF>
             
              <!--- check to see if there are images for this horse yet, if not, make this image a rep and weight = 1 --->
              <CFQUERY NAME="itemResults" DATASOURCE="#dsn#">
                  SELECT MAX(weight) AS weight
                  FROM item_image
                  WHERE item_id = <CFQUERYPARAM VALUE="#ARGUMENTS.itemId#" CFSQLTYPE="CF_SQL_INTEGER">;
              </CFQUERY>
             
              <!--- insert the image info and increment the weight --->
              <CFIF itemResults.weight EQ "">
                  <CFSET rep = 1>
                  <CFSET weight = 1>
              <CFELSE>
                  <CFSET rep = 0>
                  <CFSET weight = itemResults.weight + 1>   
              </CFIF>
             
              <CFQUERY NAME="insertResults" DATASOURCE="#dsn#">
                  INSERT INTO item_image (item_id, filename, rep, weight)
                  VALUES (
                      <CFQUERYPARAM VALUE="#ARGUMENTS.itemId#" CFSQLTYPE="CF_SQL_INTEGER">,
                      <CFQUERYPARAM VALUE="#imageResult.serverFile#" CFSQLTYPE="CF_SQL_VARCHAR">,
                      <CFQUERYPARAM VALUE="#rep#" CFSQLTYPE="CF_SQL_SMALLINT">,
                      <CFQUERYPARAM VALUE="#weight#" CFSQLTYPE="CF_SQL_INTEGER">);
              </CFQUERY>
             
              <!--- create the resized versions --->
              <CFSET index = 1>
              <CFLOOP LIST="#imageSizes#" INDEX="y">
                  <CFIMAGE SOURCE="#imageDir#\original\#imageResult.serverFile#"
                      ACTION="resize"
                          WIDTH="#ListFirst(y,'|')#"
                              HEIGHT="#ListLast(y,'|')#"
                                  DESTINATION="#imageDir#\#ListGetAt(imageFolders, index)#\#imageResult.serverFile#"
                                      OVERWRITE="Yes">
                  <CFSET index = index + 1>
              </CFLOOP>
          <CFCATCH TYPE="any">
              <CFSET report_issue()>
          </CFCATCH>
          </CFTRY>
             
      </CFFUNCTION>