2
<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" />

目前我正在使用 Cffileupload 批量上传文件。我能够创建在 SQL 数据库中创建两个新条目的 INSERT SQL 语句。不幸的是,我未能确定如何也插入文件名。有谁知道有效插入文件名的方法?正如您将在下面看到的,我没有有效地使用 Cffile.Server,希望也将文件名插入数据库。任何见解都会受到欢迎。

错误代码:CFFILE 中未定义元素 SERVERFILE。

4

2 回答 2

2

<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>
于 2012-12-27T04:29:42.430 回答
-1

要获取文件名的值,请不要使用 cffile.serverfile。尝试使用 file.serverfile。由于某种原因,CF 将对象名称默认为“文件”。不要忘记从文件名中删除空格和非法字符。我使用StripAllBut删除所有坏字符。

于 2012-12-27T15:54:39.647 回答