Lauren Robinson
Lauren Robinson

Reputation: 443

ColdFusion 9 and filestream

I have an application that allows users to upload files. Right now it is uploading directly to the file system. I have my database set up to use filestream (in SQL Server 2008). I have a form with an input field type of file.

<input type="file" name="ul_path1" id="ul_path1">

that allows the user to select the file. I then tried to use the files name selected to do a query to insert the file into the database.

<cfquery datasource=#ODSN# name="upFiles">
insert into redbook_uploads 
'#session.buildno#', '#form.ul_path1#', Cast('#form.ul_path1#' As varbinary(max))
</cfquery>

But I get an error right before #form.u1_path1# and in the error message it shows the SQL statement as

insert into redbook_uploads '009', 'C:\apps\ColdFusion9\runtime\servers\coldfusion\SERVER-INF\temp\wwwroot-tmp\neotmp7306602622243140924.tmp', Cast('C:\apps\ColdFusion9\runtime\servers\coldfusion\SERVER-INF\temp\wwwroot-tmp\neotmp7306602622243140924.tmp' As varbinary(max))

when it should be:

insert into redbook_uploads '009', 'C:\users\username\file.pdf', Cast('C:\users\username\file.pdf' As varbinary(max))

I can't figure out how to get the actual file name and am not sure why it's not getting it.

Upvotes: 0

Views: 824

Answers (2)

Leigh
Leigh

Reputation: 28873

For whatever reason, the path to the file is defaulting to:

C:\apps\ColdFusion9\runtime\servers\coldfusion\SERVER-INF\temp\wwwroot-tmp\neotmp###.tmp.

We need to see some code to provide a more specific answer. But to answer part of your question ...

Newly uploaded files are always placed in a server temp directory and given a temporary file name. On your action page, you must use cffile action=upload tag to move the file from the temp directory to the desired location.

The action name is a bit misleading. It does not really upload anything. As you can see from the temporary path, the file was already uploaded to the server. All action=upload does is to move/rename the file. It also populates the CFFILE (or result) structure with details about the file, such as it is name, file extension, mime type, etcetera. (For more details, see the documentation.) Those values can be used to save file information to your database.

   <cffile action="upload" 
          fileField    = "form.nameOfYourInputField"  
          destination  = "c:\path\to\target\directory"  
          nameConflict = "makeUnique"
          result="fileDetails" /> 

   <cfdump var="#fileDetails#" label="Uploaded File Details">

Upvotes: 2

Adrian J. Moreno
Adrian J. Moreno

Reputation: 14859

This forum post might have the correct answer:

Here are the basic steps for uploading a file and storing it in your database.

  1. Create a page with a form that includes a file tag. This form will submit to an 'action.cfm' page. Note the name of the .cfm page is not important, 'action.cfm' is an example.

  2. On your 'action.cfm' use CFFILE with action="upload" tag to save the file to your filesystem.

  3. Use the CFFILE tag with action="readbinary" to read the contents of the uploaded file into a variable. You may also want to use CFFILE to delete the uploaded file after it has been read into a variable.

  4. Use CFQUERY with CFQUERYPARAM to insert the file conents.

Upvotes: 2

Related Questions