Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

UPloading attachments

Posted on 2006-11-22
11
Medium Priority
?
261 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 25

Assisted Solution

by:dgrafx
dgrafx earned 1000 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
Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

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…
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.
Integration Management Part 2
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …

885 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