Skip to main content
Known Participant
August 23, 2010
Question

cffileupload and inserting a record to a database

  • August 23, 2010
  • 1 reply
  • 1963 views

I'm really hoping someone can help me with this as I've spent hours trawling through all the currently available information and haven't been able to get this to work.  I want to include the new cffileupload tool on the property site I'm working on but I need to be able to upload the photos and enter a new record into the database connecting those photos with the property they relate to so that they will then be served up on the property details page.  I have a version that works using cffile upload but not the new interface.  I'm also quite new to ColdFusion, so learning all the time.  Here's the code that doesn't work:

FORM FILE:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en-UK">

<head>

</head>

<body>

<cfform action="cffileupload_action.cfm">

<!--- Choose Property to Upload Photos for --->

<div id="chooseproperty">

<fieldset id="chooseproperty">

<legend><span>Choose Property</span></legend>

<cfselect name="propertyname" query="propertylist" display="propertyname" queryposition="below"></cfselect><br />

</fieldset>

</div>

</cfform>

<!--- Choose Photos to Upload --->

<div id="photoupload">

<fieldset id="photoupload">

<legend><span>Upload Photos</span></legend>

<cffileupload url="cffileupload_action.cfm?#urlEncodedFormat(session.urltoken)#" extensionfilter=".jpg, .png, .jpeg" maxuploadsize="1" title="Photo Uploader" width="600" addbuttonlabel="Select Photos" clearbuttonlabel="Remove All Photos" deletebuttonlabel="Remove Photo" />

<input type="submit" value="Submit" id="controls">

</fieldset>

</div>

</div><!-- end of content div -->

</body>

</html>

ACTION FILE:

<cfquery name="lookupID" datasource="BKtest">

SELECT propID

FROM properties

WHERE propertyname = "#Form.propertyname#"

</cfquery>

<cffile action="uploadall" destination="/Applications/MAMP/htdocs/beckleykenny/images/properties" nameConflict="Overwrite" />

<cfquery name="addfilename" dataSource="BKtest">

INSERT INTO images(imagefilename, propID)

VALUES('#CFFILE.SERVERFILE#', '<cfoutput query="lookupID">#propID#</cfoutput>')

</cfquery>

HOWEVER, this works so it's obviously something to do with the presence of the query & the output of that query.  What happens is that the upload won't work.  This works (I've just substituted the query output value for the actual real value of the ID in the database for testing purposes):

<cffile action="uploadall" destination="/Applications/MAMP/htdocs/beckleykenny/images/properties" nameConflict="Overwrite" />

<cfquery name="addfilename" dataSource="BKtest">

INSERT INTO images(imagefilename, propID)

VALUES('#CFFILE.SERVERFILE#', '2')

</cfquery>

In order for it to be reasonably foolproof, it has to be a <cfselect> so that the propertyname can be correctly chosen from the database, though I couldn't get it to work when I tried making it an ordinary <form> and substituting an <input> field so that the propertyname could be typed in. Submitting that form simply resulted in the file being uploaded but when the form was submitted, an error was generated saying that there wasn't any file uploaded to associate with the form data!

I sincerely hope someone can help me work out why it's not working.  I know it's something to do with having the query in the action page but I can't see how I'm going to fetch the id of the property any other way.

    This topic has been closed for replies.

    1 reply

    Participating Frequently
    August 23, 2010

    In what way it doesn't work ? Do you get an error ? If yes check the

    ColdFusion log files (#CF#/logs/ for standard install).

    --

    Mack

    Known Participant
    August 23, 2010

    When I click Upload on the UI, it gives a Status Code:500.  Checking the logs (3 logs appear to have entries in them, all the same I believe), this is what it says:

    Element PROPERTYNAME is undefined in FORM. The specific sequence of files included or processed is: /Applications/MAMP/htdocs/beckleykenny/cffileupload_action.cfm, line: 4

    Previous entries reflect the same when it was Session.propertyname or Url.propertyname

    .. which makes sense but I don't know how to solve the issue.

    Participating Frequently
    August 23, 2010

    I'm not familiar with cfuploadfile but I've used SWFUpload and

    cffileupload looks similar to that. The problem is that cffileupload

    does just that, it uploads the file, without sending the additional

    form elements in your form (like propertyName). So you'll need to

    first send the propertyName in the URL to the server and the easy way

    would be like this (assuming the jQuery library):

    $("##propertyName").change(function() {

    // Force the form to submit so we can have the propertyName in ColdFusion

    $(this).form.submit();

    })

    ....

    output the file upload with the correct propertyName in the URL

    cffileupload url="cffileupload_action.cfm?#urlEncodedFormat(session.urltoken)#&propertyName=#url.propertyName#"

    ....

    --

    Mack