Skip to main content
Participant
July 3, 2019
Answered

How to get file path from <cfinput type="file">

  • July 3, 2019
  • 3 replies
  • 1316 views

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.

<cfform  action="">

   <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!

This topic has been closed for replies.
Correct answer BKBK

<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>

3 replies

BKBK
Community Expert
BKBKCommunity ExpertCorrect answer
Community Expert
July 3, 2019

<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>

Participant
July 4, 2019

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?

BKBK
Community Expert
Community Expert
July 4, 2019

There is a tag for multiple uploads:<cffileupload>. I gave a worked-out code example of <cffileupload> in a previous thread.

WolfShade
Legend
July 3, 2019

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,

^ _ ^

Legend
July 3, 2019

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>