Skip to main content
Inspiring
February 19, 2009
Question

OO/Framework, Uploading Images Issue

  • February 19, 2009
  • 7 replies
  • 488 views
I am using using a form that uploads a file. If I use a self calling form
that
reinitializes the form values from the bean cfc, my field contatining the
image no longer recognizes it as an image and the
cffile fails.

<cfif isdefined('form.staffid')>
<cfoutput> in isdefined staff </cfoutput>
<cfset staffmember = createobject('component',
'datafoo.cfc.staff').init(form)>
<!--- check for errors before saving --->

<cflock name="photolock" timeout="30">
<cffile action= "upload"
filefield='staffmember.filetoupload'
destination="#expandpath(arguments.directory)##staffmember.filename#"
nameconflict="makeunique">
</cflock
</cfif>

I get the error: The form field staffmember.filetoupload did not contain a
file.


However, if I take that form and instead of recreating the instance from the
bean.init,
just run the upload using the form variable, the image gets uploaded fine

<cfif isdefined('form.staffid')>
<cfoutput> in isdefined staff </cfoutput>
<!--- IGNORE <cfset staffmember = createobject('component',
'datafoo.cfc.staff').init(form)> --->
<!--- check for errors before saving --->

<cflock name="photolock" timeout="30">
<cffile action= "upload"
filefield='file.filetoupload' <!--- *** use form var here -
works --->
destination="#expandpath(arguments.directory)##form.filename#"
nameconflict="makeunique">
</cflock
</cfif>


Any suggestions?
In the bean init I have

<cfcomponent >
<cfproperty name="filetoupload" type="string" default="">

<cfscript>
//Initialize the CFC with the default properties values.
this.filetoupload='';
</cfscript>

<cffunction name="init" output="false" returntype="staff">
<cfargument name="stValues" required="no" type="struct">
<cfif isdefined('arguments.stValues')>
<cfset this.filetoupload = arguments.stValues.filetoupload>
<cfreturn this>
</cffunction>
</component>


Any suggestions?

--
Tami Burke
Honey House Web Designs


    This topic has been closed for replies.

    7 replies

    Inspiring
    February 26, 2009
    Thanks BKBK, this is actually what I have pretty close to in the code... I
    just cut out all the
    other variables, and rewrote it for the post to make it easier to read.

    I will also try the double quotes. as Daverms suggested.

    WIll keep you posted,
    Tami
    "BKBK" <webforumsuser@macromedia.com> wrote in message
    news:go02i3$cva$1@forums.macromedia.com...
    | You can simplifyy and clean the code up so as to get something like:
    |
    | <cfif isdefined('form.staffid')>
    | <cfset staffmember = createobject('component',
    'datafoo.cfc.staff').init(form)>
    | <!--- check for errors before saving --->
    | <cffile action= "upload" filefield='filetoupload'
    | destination="#expandpath(arguments.directory)##staffmember.filename#"
    | nameconflict="makeunique">
    | </cfif>
    |
    | <cfcomponent >
    | <cfset variables.filetoupload = "">
    | <cffunction name="init" output="false" returntype="staff">
    | <cfargument name="stValues" required="no" type="struct">
    | <cfset variables.filetoupload = arguments.stValues.filetoupload>
    | <cfreturn this>
    | </cffunction>
    | </component>
    |
    |
    |
    |


    BKBK
    Community Expert
    Community Expert
    February 24, 2009
    You can simplify and clean up the code so as to get something like:

    <cfif isdefined('form.staffid')>
    <cfset staffmember = createobject('component', 'datafoo.cfc.staff').init(form)>
    <!--- check for errors before saving --->
    <cffile action= "upload" filefield='filetoupload' destination="#expandpath(arguments.directory)##staffmember.filename#" nameconflict="makeunique">
    </cfif>

    <cfcomponent >
    <cfset variables.filetoupload = "">
    <cffunction name="init" output="false" returntype="staff">
    <cfargument name="stValues" required="no" type="struct">
    <cfset variables.filetoupload = arguments.stValues.filetoupload>
    <cfreturn this>
    </cffunction>
    </component>


    Inspiring
    February 24, 2009
    Hi,

    It seems your fieldfield variable (of your <cffile> tag) is placed inside single quotes, try chaning it to double quotes.

    Also if you have pound signs surrounding it, remove it and have a try again.

    HTH
    Inspiring
    February 23, 2009
    Yep, got that too. This is an old application that I am rewriting during a
    'facelift' project so the forms
    all exist and work.

    I am using the Beans/Dao/Gateway that is originally created in CFEclipse and
    customizing to manage
    validation other custom issues.

    The bean sets the original variable using cfproperty tag
    <cfproperty name='filenamehere' type='string' default=''>

    Then in the init( obj ) function, the form structure is passed into the init
    function (as obj structure) and the
    this.filenamehere variable is loaded from the form.
    <cfscript>
    this.filenamehere =...;
    </cfscript>
    This is where I have attempted putting the form variable containing the
    file. I have tried it as
    this.filenamehere = obj.filenamehere; <-- no good
    this.filenamehere = '#obj.filenamehere#'; <-- no good
    this.filenamehere='obj.filenamehere' <-- no good

    <sigh>
    Tami

    "Daverms" <webforumsuser@macromedia.com> wrote in message
    news:gnu295$q52$1@forums.macromedia.com...
    | Hi Tami,
    |
    | Please check your <form> tag and make sure it has the enctype attribute
    set to,
    |
    | "enctype= "multipart/form-data"
    |
    | HTH


    Inspiring
    February 23, 2009
    Hi Tami,

    Please check your <form> tag and make sure it has the enctype attribute set to,

    "enctype= "multipart/form-data"

    HTH
    Inspiring
    February 23, 2009
    I tried it with quotes, with quotes & #s and w/o quotes. and it fails each
    time. I suspect the issue
    rises from the bean's assignment converting the variable from a file to a
    "string" attribute. However,
    So, in this case I had to break my OO rule and brute force the file upload
    via the form variable
    instead of using a bean.
    Thanks JR for the thoughts, I agree with your sentiments.
    TAmi

    "JR Bob Dobbs" <webforumsuser@macromedia.com> wrote in message
    news:gnp9jd$rp9$1@forums.macromedia.com...
    | CFFILE expects the filefield attribute to refer to a form field name. I
    | suspect that the string "staffmember.filetoupload" is not recognized as a
    form
    | field name. You might also try filefield='#staffmember.filetoupload#' if
    the
    | variable filetoupload conatins a form field name. Note that the docs
    specify
    | that pound signs should not be used in the filefield attribute so I'm not
    sure
    | this will work.
    |


    Inspiring
    February 21, 2009
    CFFILE expects the filefield attribute to refer to a form field name. I suspect that the string "staffmember.filetoupload" is not recognized as a form field name. You might also try filefield='#staffmember.filetoupload#' if the variable filetoupload conatins a form field name. Note that the docs specify that pound signs should not be used in the filefield attribute so I'm not sure this will work.