jayron
jayron

Reputation: 69

Coldfusion 10 cffileupload insert file name

<cfquery name="dirs" datasource="devsite" result="mySillyLittleResult">
        INSERT INTO folders (name)
        VALUES ('New Folder')
    </cfquery>

    <cfset fileId = mySillyLittleResult["GENERATEDKEY"]>

    <cfquery name="file" datasource="helloworld" result="anotherSillyLittleResult">
        INSERT INTO myfiles (id, filename)
        VALUES ('#fileId#', '#Cffile.ServerFile#')
    </cfquery>

    <cffileupload  
                    url="upload.cfm" 
                    progressbar="true" 
                    name="myupload" 
                    addButtonLabel = "Add File" 
                    clearButtonlabel = "Clear it" 
                    hideUploadButton = "false" 
                    width=600 
                    height=400 
                    title = "File Upload" 
                    maxuploadsize="20" 
                    extensionfilter="*.jpg, *.png, *.flv, *.txt" 
                    BGCOLOR="##FFFFFF" 
                    MAXFILESELECT=10 
                    UPLOADBUTTONLABEL="Upload now" align="center" />

Currently I am using Cffileupload to batch upload files. I am able to create INSERT SQL statements that create two new entries in a SQL database. Unfortuneately, I have been unsuccessful in determining how to also insert a file name as well. Does anyone perhaps know a way to effectively insert the file name? As you will see below, I'm using Cffile.Server ineffectively in hope of also inserting the file name into the database. Any insight would be welcomed.

Error Code: Element SERVERFILE is undefined in CFFILE.

Upvotes: 2

Views: 1814

Answers (2)

Sollinger04
Sollinger04

Reputation: 405

To get the value of the file name, you don't use cffile.serverfile. Try using file.serverfile. For some reason CF defaults the object name to "file". Don't forget to remove spaces and illegal characters from your file name. I use StripAllBut to remove all bad characters.

Upvotes: -1

genericHCU
genericHCU

Reputation: 4446

<cffileupload> is only the form control, like <input type = "file">, you still need to create a handler that uses <cffile action = "upload" (or action = "uploadall")>. Your error is because there is no cffile structure due to no <cffile> tag (see cffile docs).

per the example in the cffileupload docs.

<cfif isdefined("form.FIELDNAMES")> 
      <cffile action = "upload" destination = "#ExpandPath('.')#" nameconflict="makeunique"> 
</cfif> 
<cffileupload name="myuploader">

There is a second example in the docs that will be more helpful. It is long winded so I'm not including it here so please read the linked docs.

you would want to put your <cffile> tag above your queries, like so

<cfif structKeyExists(form, "fieldNames")>
  <cffile action = "upload" destination = "#ExpandPath('[your upload folder]')#" nameconflict="makeunique">
  <cfquery name="dirs" datasource="devsite" result="mySillyLittleResult">
    INSERT INTO folders (name)
    VALUES ('New Folder')
  </cfquery>

  <cfset fileId = mySillyLittleResult["GENERATEDKEY"]>
  <!--- I assume it's the same database with the same data source? --->
  <!--- Changed "helloworld" to "devsite" --->
  <cfquery name="file" datasource="devsite" result="anotherSillyLittleResult">
    INSERT INTO 
      myfiles (id, 
               filename)
      VALUES (<cfqueryparam cfsqltype="cf_sql_integer" value="#fileId#">, 
              <cfqueryparam cfsqltype="cf_sql_varchar" value="#Cffile.ServerFile#">)
  </cfquery>
</cfif>

Upvotes: 2

Related Questions