Solved

UPloading attachments

Posted on 2006-11-22
11
249 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 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
PHP in Apache server 20 95
question about access to website 2 77
Website URL redirection 10 69
web & database SERVERS -- PHYSICAL & VM ? 5 57
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.
When it comes to showing a 404 error page to your visitors, you do not want that generic page to show, and you especially do not want your hosting provider’s ad error page to show either. In this article, I will show you how to enable the custom 40…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

809 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