Solved

Upload file Coldfusion and inputing file path into DB

Posted on 2006-11-01
5
210 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

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

Introduction This article explores the design of a cache system that can improve the performance of a web site or web application.  The assumption is that the web site has many more “read” operations than “write” operations (this is commonly the ca…
Lease-to-own eliminates the expenditure of hardware replacement and allows you to pay off the server over time. Usually, this is much cheaper than leasing servers. Think of lease-to-own as credit without interest.
The purpose of this video is to demonstrate how to automatically show related posts at the bottom of a blog post in WordPress. This will be demonstrated using a Windows 8 PC. Plugin “Yet Another Related Posts Plugin” will be used. Go to your…
The purpose of this video is to demonstrate how to add AdSense Ads to a WordPress Website, and how to set up WordPress to automatically place Ads in Sidebars. This will be demonstrated using a Windows 8 PC. Log into your AdSense account. : Cli…

828 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