Solved

Upload file Coldfusion and inputing file path into DB

Posted on 2006-11-01
5
207 Views
Last Modified: 2013-12-24
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:
http://www.experts-exchange.com/Web/WebDevSoftware/ColdFusion/Q_21293970.html?query=get+filename+from+upload+file+ColdFusion&clearTAFilter=true

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

please help!
0
Comment
Question by:MetDia
  • 3
5 Comments
 
LVL 20

Expert Comment

by:trailblazzyr55
ID: 17852990
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...
0
 
LVL 20

Accepted Solution

by:
trailblazzyr55 earned 500 total points
ID: 17853008
Sorry that was not correct, I forgot to add the correct insert syntax to what you had ;o)

forgot the insert INTO part, this should work better...

<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 into 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>
0
 
LVL 39

Expert Comment

by:gdemaria
ID: 17853099

>  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#')

0
 
LVL 20

Expert Comment

by:trailblazzyr55
ID: 17853149
it'd still need the proper insert syntax

INSERT INTO [TABLENAME] ([Columns])
VALUES ([Column Values])
0
 

Author Comment

by:MetDia
ID: 17857583
got it!!!
thanks so much guys!
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this short web based tutorial, I wanted to show users how they can still use the powers of FrontPage in conjunction with Expression Web 3.  Even though Microsoft eliminated the use of Web components, we can still use them with FrontPage and edit …
If you don't have the right permissions set for your WordPress location in IIS, you won't be able to perform automatic updates. Here's how to fix the problem.
The purpose of this video is to demonstrate how to insert an Iframe into WordPress. This will be demonstrated using a Windows 8 PC. Go to your WordPress login page. This will look like the following: mywebsite.com/wp-login.php : Open Page or Post…
The purpose of this video is to demonstrate how to exclude a particular blog category from the main blog page. This is can be used when a category already has its own tab, or you simply want certain types of posts not to show up on the main blog. …

919 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now