Link to home
Start Free TrialLog in
Avatar of MetDia
MetDia

asked on

Upload file Coldfusion and inputing file path into DB

Hi experts!

I need some help with an upload of file in Coldfusion.

My goal is to create a system where a user will input some text and "attach" files ... send this text into a DB and send the path to the file uploaded into the DB.
so later on, I can query that DB and use that path to put up links on the website to those files.

I basically have a form:
<form action="Submit.cfm" enctype="multipart/form-data" method="post" name="myForm">
<textarea cols="100" name="text" rows="10"></textarea>
<input name="myFile1" type="file" />
<input name="myFile2" type="file" />
</form>

and in the submit page:
<cffile action="UPLOAD" nameconflict="overwrite" filefield="myFile1" destination="i:\websites\documents">
<cffile action="UPLOAD" nameconflict="overwrite" filefield="myFile2" destination="i:\websites\documents">
<cfquery name="insert" datasource="DB">
insert tablename
values('#text#','i:\websites\documents\#cffile.clientfile#')
</cfquery

I tried looking at the answer:
https://www.experts-exchange.com/questions/21293970/Getting-the-file-name.html?query=get+filename+from+upload+file+ColdFusion&clearTAFilter=true

but it still not working for me...
I'm using CF mx

please help!
Avatar of trailblazzyr55
trailblazzyr55

you're missing a few things in your code, and since you have two CFFILE's it'll get the data mixed up for the insert to you query...

try something like this...

<cfset myFileFormList = "myFile1,myFile2"> <!--- loop over the fields you want to insert/upload --->
<cfloop list="#myFileFormList#" index="myField">
      <cffile action="UPLOAD" nameconflict="overwrite" filefield="#myField#" destination="i:\websites\documents">
      <cfquery name="insert" datasource="DB">
      insert tablename (ColumnNameOne,ColumnNameTwo) <!--- don't forget to add the columns you want to insert to here --->
      values('#text#','i:\websites\documents\#cffile.clientfile#') <!--- then set the values you want to insert to those columns here --->
      </cfquery>
</cfloop>

now you have a unique CFFILE and you're query will work... you were also forgetting a closing bracket on your query that you posted...
ASKER CERTIFIED SOLUTION
Avatar of trailblazzyr55
trailblazzyr55

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gdemaria

>  insert tablename
>  values('#text#','i:\websites\documents\#cffile.clientfile#')

Seems you want to save to the table the location of the file on the server which would be #cffile.serverFile#
clientFile is the name of the original file from the PC; often they are the same.  

(( But you may want to end up changing the stored file name in the future if some of the file names are causing grief in your web browser.  For example, when you reference the file to download, if the file name contains characters that are not browser friendly it may be a challenge.  I usually store files in a "clean" name and also save the client file name in the table.  Then when downloading grab the file and give it to the user with the saved client file name.))


Also, I would not hard code the path in the insert, the #cffile.serverDirectory# variable will tell you where it ends up..

You have already told CF where to put the file in this command..

<cffile action="UPLOAD" nameconflict="overwrite" filefield="myFile1" destination="i:\websites\documents">

Try this...

  insert tablename
  values('#text#','#cffile.serverDirectory#\#cffile.serverFile#')

it'd still need the proper insert syntax

INSERT INTO [TABLENAME] ([Columns])
VALUES ([Column Values])
Avatar of MetDia

ASKER

got it!!!
thanks so much guys!