Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 263
  • Last Modified:

UPloading attachments

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
nmarano
Asked:
nmarano
  • 6
  • 4
2 Solutions
 
dgrafxCommented:
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
 
trailblazzyr55Commented:
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
 
nmaranoAuthor Commented:
Thanks,

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

Nick
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
nmaranoAuthor Commented:
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
 
trailblazzyr55Commented:
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
 
trailblazzyr55Commented:
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
 
nmaranoAuthor Commented:
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
 
trailblazzyr55Commented:
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
 
nmaranoAuthor Commented:
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
 
trailblazzyr55Commented:
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
 
trailblazzyr55Commented:
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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 6
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now