Solved

UPloading attachments

Posted on 2006-11-22
11
247 Views
Last Modified: 2013-12-24
Experts,

Can someone tell me if there is a way to allow users to  upload attachments from my app to my server?  Users are answering questions ,and one particular question asks them to upload a file.  I would like to store all of the answers along with the file that they uploaded in 1 table if that is possible.  Any suggestions would be appreciated.

Thanks
Nick
0
Comment
Question by:nmarano
  • 6
  • 4
11 Comments
 
LVL 24

Assisted Solution

by:dgrafx
dgrafx earned 250 total points
ID: 17996869
you need to add the ENCTYPE="multipart/form-data" as below and the type="file" field
<form action="actionpage.cfm" ENCTYPE="multipart/form-data" method="post" name="Form">
<INPUT TYPE="FILE" NAME="AttachFile">
<INPUT TYPE="Submit">
</form>

this is a basic form actionpage.cfm
<cfset filename=""><!--- this is a default so that if file is not uploaded --->
<cfif StructKeyExists(form,"AttachFile") and len(trim(form.AttachFile))>
<cfset filelocation="d:\web\files\"><!--- this is just the absolute path to where the file will go --->
<CFFILE ACTION="UPLOAD"
FILEFIELD="AttachFile"
DESTINATION="#filelocation#"
NAMECONFLICT="makeunique"><!--- skip or overwrite are other options for nameconflict --->
<cfset filename=cffile.serverfile>
</cfif>

then when you insert form data to db - '#filename#' is what you'd insert for the name of the file they uploaded
0
 
LVL 20

Expert Comment

by:trailblazzyr55
ID: 17998173
to add to what dgrafx mentioned in regards to storing that image data with your other related question data...

>I would like to store all of the answers along with the file that they uploaded in 1 table if that is possible.

you'd want to store the absolute path, or if the absolute path will always be the same, store the directory name and image name/ext, in your database along with whatever questions info it relates to in your database...

for instance, storing the filename and filelocation along with your question info...

<cfquery name="youQry" datasource="">
  INSERT INTO tableName ( [tableColumnsHereForOtherValues], fileName, filePath )
  VALUES ( [otherValuesAssociatedToQues], '#filename#', '#filelocation#' )
</cfquery>

this way the image is stored on your server, and you have the location stored along with your other question info stored in your table...

you could then query that info later and the table will have the data to display that image where you need it based on the fileName and fileLocation stored with your question info...
0
 
LVL 2

Author Comment

by:nmarano
ID: 18000398
Thanks,

I will take a look at this and get back to you both.  Thanks again

Nick
0
 
LVL 2

Author Comment

by:nmarano
ID: 18020632
dgrafx, trail

Thank you both for your suggestions, I almost have this working.  I am setting up a small test page to try and get this to work, and was hoping for a little more help.  Trail, this may sound as a really dumb question, but where you have fileName, filePath in the insert statment, am I supposed to put the actual filePath where I want this to be stored?  I tried running it, but got this error

Error Executing Database Query.  
Syntax error or access violation: You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near '[fname,lname,email,attach], fileName, filePath ) VALUES ( [f  
 
The error occurred in C:\Inetpub\wwwroot\nmarano\Code\attachment_action.cfm: line 13
 
11 : <cfquery name="youQry" datasource="APTVRE">
12 :   INSERT INTO attachFile ( [fname,lname,email,attach], fileName, filePath )
13 :   VALUES ( [fname,lname,email,AttachFile], '#filename#', '#filelocation#' )
14 : </cfquery>

 

--------------------------------------------------------------------------------
 
SQL    INSERT INTO attachFile ( [fname,lname,email,attach], fileName, filePath ) VALUES ( [fname,lname,email,AttachFile], 'cross1.jpg', 'c:\NickAttach\' )  
DATASOURCE   APTVRE
 

My action page looks like this...........

<cfset filename="">
<cfif StructKeyExists(form,"AttachFile") and len(trim(form.AttachFile))>
<cfset filelocation="c:\NickAttach\">
<CFFILE ACTION="UPLOAD"
FILEFIELD="AttachFile"
DESTINATION="#filelocation#"
NAMECONFLICT="makeunique">
<cfset filename=cffile.serverfile>
</cfif>

<cfquery name="youQry" datasource="APTVRE">
  INSERT INTO attachFile ( [fname,lname,email,attach], fileName, filePath )
  VALUES ( [fname,lname,email,AttachFile], '#filename#', '#filelocation#' )
</cfquery>


Thanks
nick
0
 
LVL 20

Expert Comment

by:trailblazzyr55
ID: 18020687
It's due to the []'s in the SQL, I just had those in there for a place holder, they should not be in the code, try this...

<cfset filename="">
<cfif StructKeyExists(form,"AttachFile") and len(trim(form.AttachFile))>
<cfset filelocation="c:\NickAttach\">
<CFFILE ACTION="UPLOAD"
FILEFIELD="AttachFile"
DESTINATION="#filelocation#"
NAMECONFLICT="makeunique">
<cfset filename=cffile.serverfile>
</cfif>

<cfquery name="youQry" datasource="APTVRE">
  INSERT INTO attachFile (fname, lname, email, fileName, filePath )
  VALUES ('#form.fname#', '#form.lname#', '#form.email#', '#filename#', '#filelocation#' )
</cfquery>
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 20

Expert Comment

by:trailblazzyr55
ID: 18020705
you also don't need "AttachFile" in your table, the "fileName" should be all you need, the "AttachFile" is the file you're uploading, however due to the ENCTYPE="multipart/form-data" it will not be of much use, it's more used for the <cffile>, the "fileName" and "fileLocation" is all you'd need to reference that file at a later time...
0
 
LVL 2

Author Comment

by:nmarano
ID: 18020770
Trail,

Sorry about that, I realized it after I had posted, but I am still getting an error.  The error I get is................

Error Executing Database Query.  
Syntax error or access violation: You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near ''c:\NickAttach\' )' at line 2  
 
The error occurred in C:\Inetpub\wwwroot\nmarano\Code\attachment_action.cfm: line 13
 
11 : <cfquery name="youQry" datasource="APTVRE">
12 :   INSERT INTO attachFile (fname, lname, email, fileName, filePath )
13 :   VALUES ('#form.fname#', '#form.lname#', '#form.email#', '#filename#', '#filelocation#' )
14 : </cfquery>
15 :


--------------------------------------------------------------------------------
 
SQL    INSERT INTO attachFile (fname, lname, email, fileName, filePath ) VALUES ('Nick', 'Marano', 'marano@emial', 'cross4.jpg', 'c:\NickAttach\' )  
DATASOURCE   APTVRE
VENDORERRORCODE   1064
SQLSTATE   42000
 
Any idea what would be throwing this error?

Thanks
nick

 
0
 
LVL 20

Accepted Solution

by:
trailblazzyr55 earned 250 total points
ID: 18020773
in regards to your question: "Trail, this may sound as a really dumb question, but where you have fileName, filePath in the insert statment, am I supposed to put the actual filePath where I want this to be stored?"

the "fileName" stores the actual file name of what was uploaded, example: "myImage.jpg"
the "filePath" stores the actual path to the file on your server, example: "c:\NickAttach\"

you save these values so later you can query this record and have the path and file name to display the file/image on your server

just before your <cffile> you're defining what directory you want to store the file in, you have it set to: "c:\NickAttach\"
you can also reference the location where the file was stored by "cffile.serverDirectory"

so you could do this as well...

<cfset filename="">
<cfif StructKeyExists(form,"AttachFile") and len(trim(form.AttachFile))>
<cfset filelocation="c:\NickAttach\">
<CFFILE ACTION="UPLOAD"
FILEFIELD="AttachFile"
DESTINATION="#filelocation#"
NAMECONFLICT="makeunique">
<cfset filename=cffile.serverfile>
<cfset filelocation=cffile.serverDirectory>
</cfif>

<cfquery name="youQry" datasource="APTVRE">
  INSERT INTO attachFile (fname, lname, email, fileName, filePath )
  VALUES ('#form.fname#', '#form.lname#', '#form.email#', '#filename#', '#filelocation#' )
</cfquery>
0
 
LVL 2

Author Comment

by:nmarano
ID: 18020828
Trail,

This last line that you added to dgrafx code, somehow did the trick

<cfset filelocation=cffile.serverDirectory>

For some reason, but adding that line, it no longer gives me an error and it writes to the db and the file gets uploaded properly.

Thank you both for the help

nick
0
 
LVL 20

Expert Comment

by:trailblazzyr55
ID: 18020844
in regards to the error, check the column type on your table to make sure it's the correct data type, example: text, varchar, etc.. depending on the type of database....

also is "attachFile" the name of your table?

also try this...

<cfquery name="youQry" datasource="APTVRE">
  INSERT INTO [attachFile] (fname, lname, [email], [fileName], [filePath] )
  VALUES ('#form.fname#', '#form.lname#', '#form.email#', '#filename#', '#filelocation#' )
</cfquery>

0
 
LVL 20

Expert Comment

by:trailblazzyr55
ID: 18020849
ahh ok, great, yeah I was thinking it was either a funky naming or value that was getting passed in...
Glad we were able to help!
good luck ;o)
~trail
0

Featured Post

Scale it in WD Gold

With up to ten times the workload capacity of desktop drives, WD Gold hard drives employ advanced technology to deliver among the best in reliability, capacity, power efficiency and performance.

Join & Write a Comment

This is a guide to setting up a new WHM/cPanel Server to be used for web hosting accounts. It is intended for web hosting company administrators and dedicated server owners. For under $99 per month (considering normal rate of Big Data Cetnters like …
Have you ever sent email via ColdFusion and thought of tracking this mail to capture the exact date and time when the message was opened ?  If yes, then this article is for you ! First we need a table user_email with columns user_id , email , sub…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

708 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

19 Experts available now in Live!

Get 1:1 Help Now