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

New Here ,
Jul 02, 2019 Jul 02, 2019

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.

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

Views

510

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct Answer

Adobe Community Professional , Jul 03, 2019 Jul 03, 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)>         <cff...

Likes

Translate

Translate
Engaged ,
Jul 03, 2019 Jul 03, 2019

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>

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jul 03, 2019 Jul 03, 2019

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,

^ _ ^

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Jul 03, 2019 Jul 03, 2019

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>

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jul 03, 2019 Jul 03, 2019

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?

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Jul 03, 2019 Jul 03, 2019

Copy link to clipboard

Copied

LATEST

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

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines