anyone can point what is wrong in my function ```<...
# lucee
g
anyone can point what is wrong in my function
Copy code
<cffunction name="handleFileUpload" access="private" returntype="any">
        <cfargument name="uploadDir" type="string" required="true">
        <cfargument name="photoField" type="any" required="false">
        
        <cfset var result = {
            files = [],
            errors = []
        }>
        
        <cfif structKeyExists(arguments, 'photoField') AND len(arguments.photoField)>
            <cfif isArray(arguments['photoField'])>
                <!--- Handle multiple file upload --->
                <cfloop array="#arguments['photoField']#" index="file">
                    <cfset var filePath = arguments.uploadDir & "/" & file.serverFile>
                    
                    <cfif file.fileSize GT 0 AND len(file.fileName)>
                        <cffile 
                            action="upload"
                            fileField="#arguments.photoField#"
                            destination="#arguments.uploadDir#"
                            nameconflict="makeunique"
                            accept="image/*"
                            result="local.uploadResult">
                        
                        <cfif structKeyExists(local.uploadResult, "serverFile")>
                            <cfset arrayAppend(result.files, {
                                fileName = file.fileName,
                                filePath = filePath,
                                serverFile = local.uploadResult.serverFile
                            })>
                        </cfif>
                    <cfelse>
                        <cfset arrayAppend(result.errors, {
                            fileName = file.fileName,
                            error = "No file selected"
                        })>
                    </cfif>
                </cfloop>
            <cfelse>
                <!--- Handle single file upload --->
                <cfset var filePath = arguments.uploadDir & "/" & arguments['photoField'].serverFile>
                
                <cfif arguments['photoField'].fileSize GT 0 AND len(arguments['photoField'].fileName)>
                    <cffile 
                        action="upload"
                        fileField="#arguments.photoField#"
                        destination="#arguments.uploadDir#"
                        nameconflict="makeunique"
                        accept="image/*"
                        result="local.uploadResult">
                    
                    <cfif structKeyExists(local.uploadResult, "serverFile")>
                        <cfset arrayAppend(result.files, {
                            fileName = arguments['photoField'].fileName,
                            filePath = filePath,
                            serverFile = local.uploadResult.serverFile
                        })>
                    </cfif>
                <cfelse>
                    <cfset arrayAppend(result.errors, {
                        fileName = arguments['photoField'].fileName,
                        error = "No file selected"
                    })>
                </cfif>
            </cfif>
        <cfelse>
            <cfset arrayAppend(result.errors, {
                fileName = "",
                error = "No files uploaded"
            })>
        </cfif>
        
        <cfreturn serializeJSON(result)>
    </cffunction>
error i am getting is: additional*Struct (ordered)*DetailstringErrorCodestring0Extended_InfostringExtendedInfostringMessagestringNo matching method for String.isSERVERFILE() found. there are no methods with this name.
q
You didn't give the full stack trace, but I'm assuming that
arguments['photoField']
is coming in as a string instead of s struct/object right after your else statement.
g
between i am passing multiple files to upload,so it should not go to the else clause
r
have you already validated the arguments are coming in as expected by doing a dump and then validate your if logic by dumping from within each if statement to confirm it is working as expected?
q
It's hard to say where the failure is, since the stack trace is cut-off, but with the limited about you gave us, it looks like it's on the else statement branch.
r
form should be
Copy code
<form method="post" enctype="multipart/form-data">
g
i did that already
s
What datatype is 'arguments.photoField'? That looks like you're passing over a string, the name of the formfield. Yet what's this line?
<cfset var filePath = arguments.uploadDir & "/" & arguments['photoField'].serverFile>
Almost certain, this is your issue:
arguments['photoField'].serverFile
Is photoField a string, or a struct? You're referencing it as both. Note the error: "No matching method for String.isServerFile()" The string in this case is arguments['photoField']. A Java String datatype doesn't have 'isServerFile' as a function.
Guaranteed that's it.
fileField="#arguments.photoField#"
It's clearly intended to be a string, trying to reference it as a struct is the issue.
I think you want
local.uploadResult.serverFile
arguments["photoField"].fileName
is going to blow up, too