Letractively / cfwheels

Automatically exported from code.google.com/p/cfwheels
0 stars 0 forks source link

fileFieldTag() not binding data to params struct #125

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Submitting a Wheels based form using a fileFieldTag() does not properly bind 
the field's file data 
to the params struct.

Example:
#startFormTag(controller="mycontroller", action="upload", 
enctype="multipart/form-data")#
    #fileFieldTag(name="thePath")#
    #submitTag(value="Upload")#
#endFormTag()#

Utilizing a CFFile in the "upload" action with the following code:

<cffile action="upload"
                    destination="#ExpandPath('\test')#"
                    nameConflict="makeunique"
                    fileField="params.thePath" 
                    >

Results in the following CF error:
The form field params.thePath did not contain a file.

Alternatively, if the CFFile's fileField attribute is changed to the FORM 
struct as follows, the CFFile 
upload succeeds.

<cffile action="upload"
                    destination="#ExpandPath('\test')#"
                    nameConflict="makeunique"
                    fileField="FORM.thePath" 
                    >

Original issue reported on code.google.com by jordanmc...@gmail.com on 29 Oct 2008 at 11:56

GoogleCodeExporter commented 9 years ago
From what I know, the fileField must match the exact name of the FORM and not 
be any variable of your choice, 
so I dont think it will be possible to do that, you must always use 
FORM.fileFieldName

Original comment by rierar...@gmail.com on 30 Oct 2008 at 12:42

GoogleCodeExporter commented 9 years ago
This seems impossible to fix. The same issue exists in Fusebox for example 
where they 
also combine form and url variables in a struct. I would recommend just leaving 
it 
unscoped instead since ColdFusion only looks in the form scope for the file 
anyway, 
i.e.:

fileField="thePath"

Maybe Wheels can automate the whole uploading process though following a 
convention 
that automatically places uploaded files in the "files" folder and returning 
the 
filename or something?

Original comment by per.djurner@gmail.com on 30 Oct 2008 at 9:36

GoogleCodeExporter commented 9 years ago

Original comment by per.djurner@gmail.com on 26 Dec 2008 at 7:36

GoogleCodeExporter commented 9 years ago

Original comment by per.djurner@gmail.com on 26 Dec 2008 at 8:32

GoogleCodeExporter commented 9 years ago
Setting back to "Accepted" so that we don't forget about the possibility of 
coding a 
Wheels convention for handling file uploads.

Original comment by per.djurner@gmail.com on 25 Aug 2009 at 4:47

GoogleCodeExporter commented 9 years ago
my first crack at this. just save this function in your model.cfc.

<cffunction name="upload">
    <cfargument name="property" type="string" required="false" default="filename">
    <cfset var loc = {}>
    <cfset loc.mp = "multipart/form-data">

    <!---
    see if the content_type is mulitpart which means a file was uploaded
    if not get out. no use wasting time
    --->
    <cfif not left(cgi.CONTENT_TYPE, len(loc.mp)) eq loc.mp>
        <cfreturn>
    </cfif>

    <!---
    when using fileField(), it expects an objectname. by convention the variable will
    be named the singualrized name of the current model

    Users -> User
    --->
    <cfset loc.obj = singularize(listlast(getmetadata().name, '.'))>

    <!--- save the property name for later use --->
    <cfset loc.property = arguments.property>
    <cfset structdelete(arguments, "property", false)>

    <!---
    by defaul, we'll look for a key in the form scope with the same name as the
    property that was passed in
     --->
    <cfset loc.field = loc.property>

    <!--- 
    we're not going to know whether the programmer used fileFieldTag() or fileField()
    to perform the upload. as such we'll look for a key in the form scope containing
    a key with what fileField() would name it, which is a concatination of the object
    and property name:

    user[lastname]

    if we find that, then we know they user fileField()
     --->
    <cfif structkeyexists(form, "#loc.obj#[#loc.field#]")>
        <cfset loc.field = "#loc.obj#[#loc.field#]">
    </cfif>

    <!--- now that we have our field name, let's look in the form scope to see if it's
blank --->
    <cfif not len(form[loc.field])>
        <cfreturn>
    </cfif>

    <!--- create the a default set of arguments to pass to cffileupload --->
    <cfset loc.args = {}>
    <cfset loc.args.action = "upload">
    <cfset loc.args.filefield = loc.field>
    <cfset loc.args.destination = expandpath(get('filePath'))>
    <cfset loc.args.nameconflict = "MAKEUNIQUE">

    <!--- append and oveerwrite the defaults with any extra arguments --->
    <cfset structappend(loc.args, arguments, true)>

    <!--- upload the file --->
    <cfset loc.cffileupload = cffileupload(
            argumentCollection=loc.args
        )>

    <cfset this[loc.property] = loc.cffileupload.serverfile>

</cffunction>

Original comment by tpetru...@gmail.com on 25 Aug 2009 at 5:56

GoogleCodeExporter commented 9 years ago
almost forgot, you will need to also download this gist, save it in your lib
directory and then include it in your model too. sorry about this, but i'm 
using the
project i'm working on to figure this out.

http://gist.github.com/138542

model.cfc

<!--- including cffileupload --->
<cfinclude template="../lib/cffileupload.cfm">

Original comment by tpetru...@gmail.com on 25 Aug 2009 at 6:01

GoogleCodeExporter commented 9 years ago
Can this be programmed as a plugin?

Original comment by per.djurner@gmail.com on 13 Nov 2009 at 1:53

GoogleCodeExporter commented 9 years ago
i was thinking about making a new controller method called upload() and 
including
this functionality. i did this in the model layer of one of my apps and it was 
too messy.

i was also thinking about doing a convention where if you named a fileField
upload<PropertyName> that it would automatically upload the file and assign the
cffile.serverfile to the params. after thinking more about it though, it would 
be
almost impossible to to support the accept attribute or make sure that file is 
of the
correct mimetype after uploading.

i think an upload method is the way to go. thoughts????

Original comment by tpetru...@gmail.com on 14 Nov 2009 at 8:40

GoogleCodeExporter commented 9 years ago
I'm liking the idea of doing it as a plugin. We can merge it into the core if 
it ends
up working really well.

Original comment by chriscle...@gmail.com on 14 Nov 2009 at 8:52

GoogleCodeExporter commented 9 years ago
closing since the UploadFiles has been available for quite sometime

http://cfwheels.org/plugins/listing/30

Original comment by tpetru...@gmail.com on 14 Aug 2010 at 5:20

GoogleCodeExporter commented 9 years ago
I love this plugin

Original comment by rierar...@gmail.com on 14 Aug 2010 at 1:22