Copy link to clipboard
Copied
Hello all,
I am a newbie to ColdFusion and got stuck in an exercise.
I am having a form where the user picks a file.
<cfinput type='file' accept='.json' name="choosefile"><br>
<cfinput type="submit" name="loadData" value="Load JSON data">
</cfform>
Now i want to read the data from file upon submit.
<cfif isdefined("form.loadData")>
<cffile action="read" file="#form.choosefile# variable="jsondataFromFile">
<cfdump var="#jsondataFromFile#">
<cfif>
Now when i run the code i get FileNotFoundException because ColdFusion is searching for file chosen in temp directory.
How do i read the file?
Please help. Thanks in advance!
<cfform enctype="multipart/form-data">
<cfinput type='file' name="choosefile"><br>
<cfinput type="submit" name="loadData" value="Load JSON data">
</cfform>
<cfset currentDirectory=expandpath('.')>
<cfif isdefined("form.loadData")>
<cffile action="upload" filefield="choosefile" destination="#currentDirectory#" accept="application/json" nameconflict="overwrite">
<cfset uploadedFilePath=currentDirectory & "\" & cffile.serverfile>
<cfif fileExists(uploadedFilePath)>
<cff
...Copy link to clipboard
Copied
After the form submission, you must upload the file to your server first (you can't read a file on a user's computer).
Try this:
<cfif isdefined("form.loadData")>
<cffile action="upload" formfield="choosefile" destination="YOUR SERVER DIRECTORY HERE">
<cfif fileExists("#serverDirectory#\#cffile.serverfile#")>
<cffile action="read" file="#serverDirectory#\#cffile.serverfile#" variable="jsondataFromFile">
<cfdump var="#jsondataFromFile#">
</cfif>
<cfif>
Copy link to clipboard
Copied
As sdsinc_ has pointed out, CF (server-side) doesn't have access to a user's computer (client-side) so can't "see" the path of the file being uploaded.
And if you are concerned about your server's disk I/O (like I am), you can actually upload the file without saving it to a particular location. As it turns out, CF _does_ automatically save the file to a TEMP folder with no execution privileges, but it will stay there only a fleeting fraction of a second, just enough time for code to tell CF where to finally save it. I've worked on projects where CF received a file without saving it to the hard drive, and instead populated a database. Zero time writing it on the CF server, zero time having to remove it once processed, no clean-up, no mess, all in RAM until done with the database.
V/r,
^ _ ^
Copy link to clipboard
Copied
<cfform enctype="multipart/form-data">
<cfinput type='file' name="choosefile"><br>
<cfinput type="submit" name="loadData" value="Load JSON data">
</cfform>
<cfset currentDirectory=expandpath('.')>
<cfif isdefined("form.loadData")>
<cffile action="upload" filefield="choosefile" destination="#currentDirectory#" accept="application/json" nameconflict="overwrite">
<cfset uploadedFilePath=currentDirectory & "\" & cffile.serverfile>
<cfif fileExists(uploadedFilePath)>
<cffile action="read" file="#uploadedFilePath#" variable="jsondataFromFile">
<cfdump var="#jsondataFromFile#">
</cfif>
</cfif>
Copy link to clipboard
Copied
Worked perfecty. Thanks a lot.
But one query. Suppose if i use multiple="multiple" property on cfinput for selecting multiple files, how do i get the array of all the selected files?
Copy link to clipboard
Copied
There is a tag for multiple uploads:<cffileupload>. I gave a worked-out code example of <cffileupload> in a previous thread.