upload throwing file does not contains a file error

51 Views Asked by At

I have a very simple code which uploads the file

<form method="post" action="upload.cfm" enctype="multipart/form-data">
    <table>
        <tbody>
            <tr>
                <td><input type="file" name="uploadFile" id="uploadFile"></td>
            </tr>
            <tr>
                <td><input type="submit" name="Upload" id="Upload" value="Upload"></td>
            </tr>
        </tbody>
    </table>
</form>  

and my upload.cfm as:

<cfif isDefined('form.upload')>
    <cfset results = uploadAndInsertData('#FORM.uploadFile#')>
</cfif>  

upload and insert function as;

<cffunction name="uploadAndInsertData" access="public" returntype="any">
    <cfargument name="uploadedFile" type="any" required="true">
    <cfset targetFolder = ExpandPath('documents')>
    <cfset success = true>

    <cfset cffile = UploadFile(
        FileField = "uploadedFile", 
        destination = "#targetFolder#"
    )>
    <cfset fileContent = fileRead("#targetFolder##uploadedFile#")>
</cffunction>

UploadFile is a function from cflib.org

https://cflib.org/udf/uploadFile

and it keeps throwing me an error,

The form field uploadedFile did not contain a file.

20 :  
21 :      <!--- Upload to temp directory. --->
22 :      <cffile action="upload" filefield="#Arguments.FileField#" destination="#Arguments.TempDirectory#" nameconflict="MakeUnique">
23 :      <cfset tempPath = ListAppend(cffile.ServerDirectory, cffile.ServerFile, "\/")>
24 :  
1

There are 1 best solutions below

0
On

The issue is with the way you are passing FileField to the function UploadFile.

FileField should be the name of the input[type="file"] which is uploadFile in your case.

<cfset cffile = UploadFile(
    FileField = "uploadFile",  <-----
    destination = "#targetFolder#"
)>

Other than that, you don't need to a call uploadAndInsertData('#FORM.uploadFile#') there is no need to pass this to the function just do uploadAndInsertData().

<cffile aciton="upload"> automatically takes the form field based on the name of the field.

You'll need to change

<cfset fileContent = fileRead("#targetFolder##uploadedFile#")>

to

<cfset fileContent = fileRead("#targetFolder##cffile.serverFIlename#")>