Hey Everyone,
Well for the last few days i been trying to figure out how to delete attachments and download attachments to my computer. The deleting is sort of working and i don't know where to begin on downloading. Right now with the deleting it will delete from the attachments folder on the server but it does not delete from the database an was wondering if someone could explain what i am doing wrong on deleting attachments and how i could make it where a user could download an attachment to there computer? Here is what i have. - <cfparam name="form.confirmed" default="0">
-
-
<cfquery name="attachment" datasource="CustomerSupport">
-
SELECT fk_ticketID,description,path
-
FROM dbo.tbl_CS_attachments
-
WHERE fk_ticketID = #URL.pk_ticketID#
-
</cfquery>
-
-
-
<form method="post">
-
<cfoutput query="ticket">
-
<input type="hidden" name="pk_ticketID" id="pk_ticketID" value="#pk_ticketID#" />
-
</cfoutput>
-
<cfoutput query="attachment">
-
<input type="hidden" name="fk_ticketID" id="fk_ticketID" value="#fk_ticketID#" />
-
</cfoutput>
-
-
<cfoutput query="attachment">
-
#description#
-
<a href="attachments/#path#" target="_blank" >view</a>
-
<br>
-
<cfif form.confirmed EQ 1>
-
<cflock timeout="60">
-
<cffile action="delete"
-
file="C:\Inetpub\Development\WWWRoot\RachelB\footprints\form\attachments\#path#">
-
</cflock>
-
</cfif>
-
<input type="hidden" name="confirmed" value="1">
-
<input type="hidden" name="fk_ticketID" value="#fk_ticketID#">
-
<input type="submit" value="delete" onClick="return confirm('Are you sure you want to delete?')">
-
</cfoutput>
-
</form>
Thank you,
Rach
Oct 17 '08
107 10839
Do you get any errors? Make sure you're testing from a fresh page, not a submitted page.
Hey Acoder,
Yep that was the problem was trying to delete a previously submitted attachment. But how could i add a message telling the user after they delete the file been deleted? an how would i go about allowing the user to download an attachment to there computer?
This is all i have for it so far - <cfoutput query="attachment">
-
<table class="attachments">
-
<tr>
-
<td>
-
#description#
-
</td>
-
<td class="view" align="center">
-
<a href="attachments/#path#" target="_blank" >view</a>
-
</td>
-
<td align="center">
-
<input type="hidden" name="confirmed" value="1">
-
<input type="hidden" name="pk_attachID" value="#pk_attachID#">
-
<input type="submit" value="delete" onClick="return confirm('Are you sure you want to delete?')">
-
</td>
-
<td align="center">
-
download
-
</td>
-
<td>
-
<cfcontent
-
file = "C:\Inetpub\Development\WWWRoot\RachelB\footprints\form\attachments\#path#"
-
type="text/html;text/plain;application/x-shockwave-flash;application/msword;image/jpeg; "
-
deleteFile = "no">
-
</td>
-
</tr>
-
</table>
-
</cfoutput>
Thank you,
Rach
You can display a message in the cfif (confirmed check) before the end tag. It's up to you if you want to put it in a div/span and style it to appear in the centre with a nice colour or whatever.
You can display a message in the cfif (confirmed check) before the end tag. It's up to you if you want to put it in a div/span and style it to appear in the centre with a nice colour or whatever.
Hey Acoder,
I think i still have a problem. Because with the delete, if i am displaying 2 files an i click on one file to delete it , it will not delete. But if i am displaying 1 file it will delete that one file, any ideas?
but what i add the message i want to display after, or would i put it somewhere in the cfif below? - <cfif form.confirmed EQ 1>
-
<cfquery name="getattachment" datasource="CustomerSupport">
-
SELECT *
-
FROM dbo.tbl_CS_attachments
-
WHERE pk_attachID=<cfqueryparam value="#form.pk_attachID#"
-
cfsqltype="cf_sql_char" maxLength="120">
-
</cfquery>
-
-
<cfquery name="deleteattachment" datasource="CustomerSupport">
-
DELETE
-
FROM dbo.tbl_CS_attachments
-
WHERE pk_attachID = #form.pk_attachID#
-
</cfquery>
-
<cflock timeout="60">
-
<cffile action="delete"
-
file="C:\Inetpub\Development\WWWRoot\RachelB\footprints\form\attachments\#getattachment.path#">
-
</cflock>
-
</cfif>
Thank you,
Rach
To answer the 2nd question first: yes, you would put it in the cfif. You may want to check that the file has indeed been deleted before displaying the messsage, so it may be an idea to attempt to delete the file first before removing the record from the database.
The delete problem is caused by the fact that all input fields are submitted. So the pk_attachID will be submitted as a list. You only need the one whose submit button was clicked. To solve this, you need to uniquely identify the attachment. You can either
1. pass it in the URL, or
2. use separate forms for each, or
3. have only one submit button, but use a checkbox to identify the attachments to be deleted.
To answer the 2nd question first: yes, you would put it in the cfif. You may want to check that the file has indeed been deleted before displaying the messsage, so it may be an idea to attempt to delete the file first before removing the record from the database.
The delete problem is caused by the fact that all input fields are submitted. So the pk_attachID will be submitted as a list. You only need the one whose submit button was clicked. To solve this, you need to uniquely identify the attachment. You can either
1. pass it in the URL, or
2. use separate forms for each, or
3. have only one submit button, but use a checkbox to identify the attachments to be deleted.
Hey Acoder,
Well how would i go about passing it in the url? mostly just confused on where i would apply the url at. I am use to doing a href= and the rest. An where in the cfif would i put it? after where i have it delete?
Thank you,
Rach
Well how would i go about passing it in the url?
You could use JavaScript (if you know that everyone's got JavaScript enabled). If you don't want to depend on JavaScript, you'll have to use one of the other two methods.
An where in the cfif would i put it? after where i have it delete?
Yes.
You could use JavaScript (if you know that everyone's got JavaScript enabled). If you don't want to depend on JavaScript, you'll have to use one of the other two methods. Yes.
Hey Acoder,
Well i guess i should go with the 3rd option where it has a checkbox next to each an then they choose the delete button. Since no clue how many attachments they will add. But anyway i have no clue what to se the checkbox to so it knows what file to delete. Here is what i have
Thank you,
Rach
You could just use the pk_attachID hidden field and set it to "checkbox" instead of "hidden". This will allow you to delete a number of attachments at once instead of one at a time. You will need to use a loop over the form.pk_attachID values for the deletion.
You could just use the pk_attachID hidden field and set it to "checkbox" instead of "hidden". This will allow you to delete a number of attachments at once instead of one at a time. You will need to use a loop over the form.pk_attachID values for the deletion.
Hey Acoder,
What type of cfloop would i use? like a cfloop query? an would i apply it to just around the checkbox or would i put the loop around the entire form?
Thank you,
Rach
No, this loop is for the actual deleting, so it would go at the top. It can replace the cfif confirmed check because you can just loop the list and if it's empty nothing is deleted. You would use a cfloop looping over a list.
No, this loop is for the actual deleting, so it would go at the top. It can replace the cfif confirmed check because you can just loop the list and if it's empty nothing is deleted. You would use a cfloop looping over a list.
Hey Acoder,
I added the list but i am not sure where i would put "del" at. would i put it at underneath file has been deleted wrapped around <cfoutput>? - <cfloop index="del" list="form.confirmed">
-
<cfquery name="getattachment" datasource="CustomerSupport">
-
SELECT *
-
FROM dbo.tbl_CS_attachments
-
WHERE pk_attachID=<cfqueryparam value="#form.pk_attachID#"
-
cfsqltype="cf_sql_char" maxLength="120">
-
</cfquery>
-
-
<cfquery name="deleteattachment" datasource="CustomerSupport">
-
DELETE
-
FROM dbo.tbl_CS_attachments
-
WHERE pk_attachID = #form.pk_attachID#
-
</cfquery>
-
<cflock timeout="60">
-
<cffile action="delete"
-
file="C:\Inetpub\Development\WWWRoot\RachelB\footprints\form\attachments\#getattachment.path#">
-
</cflock>
-
Your file has been deleted!
-
</cfloop>
Thank you,
Rach
#del# would replace #form.pk_attachID#.
Note that you need to loop over #form.pk_attachID#, not form.confirmed.
#del# would replace #form.pk_attachID#.
Note that you need to loop over #form.pk_attachID#, not form.confirmed.
Hey Acoder,
So like this correct? - <cfloop index="del" list="form.pk_attachID">
-
<cfquery name="getattachment" datasource="CustomerSupport">
-
SELECT *
-
FROM dbo.tbl_CS_attachments
-
WHERE pk_attachID=<cfqueryparam value="#del#"
-
cfsqltype="cf_sql_char" maxLength="120">
-
</cfquery>
-
-
<cfquery name="deleteattachment" datasource="CustomerSupport">
-
DELETE
-
FROM dbo.tbl_CS_attachments
-
WHERE pk_attachID = #del#
-
</cfquery>
-
<cflock timeout="60">
-
<cffile action="delete"
-
file="C:\Inetpub\Development\WWWRoot\RachelB\footprints\form\attachments\#getattachment.path#">
-
</cflock>
-
Your file has been deleted!
-
</cfloop>
Thank you,
Rach
Almost - form.pk_attachID needs to be in ##
Almost - form.pk_attachID needs to be in ##
Hey Acoder,
Well i tried that an it said error resolving parameter #Form.pk_attachID# the one in the list. Here is what i have in full.
Thank you,
Rach
That'll be because you didn't check a checkbox and to get rid of the error when no attachments are checked, you need to use cfparam for pk_attachID.
That'll be because you didn't check a checkbox and to get rid of the error when no attachments are checked, you need to use cfparam for pk_attachID.
Hey Acoder,
is this correct? - <cfloop index="del" list="<cfqueryparam value="#Form.pk_attachID#" CFSQLType = "CF_SQL_VARCHAR">">
-
<cfquery name="getattachment" datasource="CustomerSupport">
-
SELECT *
-
FROM dbo.tbl_CS_attachments
-
WHERE pk_attachID=<cfqueryparam value="#del#"
-
cfsqltype="cf_sql_char" maxLength="120">
-
</cfquery>
-
-
<cfquery name="deleteattachment" datasource="CustomerSupport">
-
DELETE
-
FROM dbo.tbl_CS_attachments
-
WHERE pk_attachID = #del#
-
</cfquery>
-
<cflock timeout="60">
-
<cffile action="delete"
-
file="C:\Inetpub\Development\WWWRoot\RachelB\footprints\form\attachments\#getattachment.path#">
-
</cflock>
-
Your file has been deleted!
-
<!---</cfif>--->
-
</cfloop>
Thank you,
Rach
Not a cfqueryparam! That goes in a cfquery. I meant cfparam like you have for form.confirmed at the top.
Not a cfqueryparam! That goes in a cfquery. I meant cfparam like you have for form.confirmed at the top.
Hey Acoder,
Here is what i have for it. Do i need to set a default? - <cfloop index="del" list="<cfparam name="#Form.pk_attachID#">">
Thank you,
Rach
Not in the list attribute. That'd be invalid syntax. At the top on its own like form.confirmed.
Not in the list attribute. That'd be invalid syntax. At the top on its own like form.confirmed.
Hey Acoder,
Ok here is what i got, this correct? -
<cfparam name="#Form.pk_attachID#">
-
<cfloop index="del" list="Form.pk_attachID">
-
<cfquery name="getattachment" datasource="CustomerSupport">
-
SELECT *
-
FROM dbo.tbl_CS_attachments
-
WHERE pk_attachID=<cfqueryparam value="#del#"
-
cfsqltype="cf_sql_char" maxLength="120">
-
</cfquery>
-
-
<cfquery name="deleteattachment" datasource="CustomerSupport">
-
DELETE
-
FROM dbo.tbl_CS_attachments
-
WHERE pk_attachID = #del#
-
</cfquery>
-
<cflock timeout="60">
-
<cffile action="delete"
-
file="C:\Inetpub\Development\WWWRoot\RachelB\footprints\form\attachments\#getattachment.path#">
-
</cflock>
-
Your file has been deleted!
-
</cfloop>
Thank you,
Rach
Still two things:
Why have you removed the ## from form.pk_attachID in cfloop list= ?
You need to set the default attribute too in the cfparam.
Hey Acoder,
is this correct?
Since it's a list, the default should be the empty string. Test it now and see if it works.
Since it's a list, the default should be the empty string. Test it now and see if it works.
Hey Acoder,
i get the error Error resolving parameter FORM.PK_ATTACHID
an it says its the cfparam line its having trouble with. here is what i have in full
Thank you,
Rach
The element name shouldn't be in ##s in cfparam.
The element name shouldn't be in ##s in cfparam.
Hey Acoder,
Yay it works!!! i just want to cry it looks so pretty. But ok so how would i do the download part? so far all i have is an it appears under the delete button - <cfcontent
-
file = "C:\Inetpub\Development\WWWRoot\RachelB\footprints\form\attachments\#path#"
-
type="text/html;text/plain;application/x-shockwave-flash;application/msword;image/jpeg; "
-
deleteFile = "no">
Thank you :),
Rach
For the download part, you need to link to another Coldfusion page which has a cfheader/cfcontent set on the page. See the documentation for those two tags to get some useful tips.
For the download part, you need to link to another Coldfusion page which has a cfheader/cfcontent set on the page. See the documentation for those two tags to get some useful tips.
Hey Acoder,
So let me get this straight from what i read, based on this link http://livedocs.adobe.com/coldfusion...gs_g-h_07.html
. Basically i would put this on the attachment page i have now the below - <cfheader name="Content-Disposition" value="attachment; filename=filename.ext">
an then on the filename.ext i would put the cfcontent correct? - <cfcontent
-
deleteFile = "yes|no"
-
file = "filename"
-
reset = "yes|no"
-
type = "file type"
-
variable = "variable name">
Thank you,
Rach
filename.ext is just an example. You should replace that with the actual file name and extension of the file to be downloaded. See some of the examples in the cfcontent doc page.
filename.ext is just an example. You should replace that with the actual file name and extension of the file to be downloaded. See some of the examples in the cfcontent doc page.
Hey Acoder,
Ok i looked at the examples on there. An ok this is all on the attachment page. Is this correct? - <cfheader name="Content-Disposition" value="attachment; filename=#path#">
-
<cfcontent
-
file = "C:\Inetpub\Development\WWWRoot\RachelB\footprints\form\attachments\#path#"
-
type="text/html;text/plain;application/x-shockwave-flash;application/msword;image/jpeg; "
-
>
-
Thank you,
Rach
You should change the type to whatever the content type is - not a whole list. Also make sure #path# is set somewhere. This would be in a new page, so the path would be passed in the URL.
You should change the type to whatever the content type is - not a whole list. Also make sure #path# is set somewhere. This would be in a new page, so the path would be passed in the URL.
Hey Acoder,
Well the reason i have a whole list is i have no clue what they will be uploading, it could be a picture,word,excel really anything so tried to leave it open. An i am confused by the second page. Would everything go on a second page? an then for the download do i need to create a url method?
Thank you,
Rach
Yes, everything would go on another page and then pass the path of the attachment to this page.
Determine the type from the extension and pass the correct type, not a list.
Yes, everything would go on another page and then pass the path of the attachment to this page.
Determine the type from the extension and pass the correct type, not a list.
Hey Acoder,
Ok so on the first page is -
<a href="C:\Inetpub\Development\WWWRoot\RachelB\footprints\form\download.cfm"><input type="submit" value="download"></a>
an then on the second page is - <cfheader name="Content-Disposition" value="attachment; filename=#path#">
-
<cfcontent
-
file = "C:\Inetpub\Development\WWWRoot\RachelB\footprints\form\attachments\#path#"
-
type="text/html;text/plain;application/x-shockwave-flash;application/msword;image/jpeg; "
-
>
do i need to on the second page wrap it all in a cfoutput?
an how would i determine the type based on the extension? didn't see anything on the doc page for cfcontent.
Thank you,
Rach
Ok so on the first page is -
<a href="C:\Inetpub\Development\WWWRoot\RachelB\footprints\form\download.cfm"><input type="submit" value="download"></a>
Don't put a submit button - just text will do. The href shouldn't be to the full path and certainly not the Windows path - just a relative path, e.g. "download.cfm". You should pass the path here, so add a parameter for path.
do i need to on the second page wrap it all in a cfoutput?
No, you don't. You'll need to get the path passed to the page. Use that in place of #path#.
an how would i determine the type based on the extension? didn't see anything on the doc page for cfcontent.
Yes, it isn't there. That was just a suggestion. You should know what the type is already. If you don't, then use the extension to determine it, e.g. ".xls" would be an Excel file, .doc would be a Word document, .jpg would be a JPEG image, etc.
Don't put a submit button - just text will do. The href shouldn't be to the full path and certainly not the Windows path - just a relative path, e.g. "download.cfm". You should pass the path here, so add a parameter for path.
No, you don't. You'll need to get the path passed to the page. Use that in place of #path#.
Yes, it isn't there. That was just a suggestion. You should know what the type is already. If you don't, then use the extension to determine it, e.g. ".xls" would be an Excel file, .doc would be a Word document, .jpg would be a JPEG image, etc.
Hey Acoder,
so is this correct for passing the path? reason i had it wrapped around a button cause wanted it to be a button (eventually) since the delete is a button would make since to make it a button as well. - <a href="download.cfm?path=#path#">download</a>
an my bad i thought i needed to use something different instead of type= to get it going on the extension.
Thank you,
Rach
To use a button instead, you can use something like: - <input type="button" name="download" value="Download" onclick="window.location.href='download.cfm?path=#path#'">
You may want to encode the path value to avoid problems with special characters.
To use a button instead, you can use something like: - <input type="button" name="download" value="Download" onclick="window.location.href='download.cfm?path=#path#'">
You may want to encode the path value to avoid problems with special characters.
Hey Acoder,
What you mean encode the path value to avoid problems? how would i do that?
Thank you,
Rach
You can use encodeURIComponent (a JavaScript function) to encode the value. In the download page, the path would be available in url.path, though you'd want to cfparam too just in case. Did you manage to work out the type of attachments? You may want to consider storing this information in the database - not necessary, but may be useful.
You can use encodeURIComponent (a JavaScript function) to encode the value. In the download page, the path would be available in url.path, though you'd want to cfparam too just in case. Did you manage to work out the type of attachments? You may want to consider storing this information in the database - not necessary, but may be useful.
Hey Acoder,
Would i do the type of attachment like so, i was not sure what to put for application/x-shockwave-flash. - <cfheader name="Content-Disposition" value="attachment; filename=#path#">
-
<cfcontent
-
file = "C:\Inetpub\Development\WWWRoot\RachelB\footprints\form\attachments\#path#"
-
type=".html,.txt,.jpg,.gif,.doc,.xls,.mdb,.ppt,application/x-shockwave-flash;"
-
>
an i am a bit confused on the encodeURIComponent. i found this http://www.w3schools.com/jsref/jsref...IComponent.asp
i am not sure if i would put this on my attachment file or my download file?.
Thank you,
Rach
No, the type should be as you had it before, but one only rather than a list. You'll have to work out what the type should be and use that one type, not a list.
encodeURIComponent is a JS function, so it would go round the #path# variable in the button code.
No, the type should be as you had it before, but one only rather than a list. You'll have to work out what the type should be and use that one type, not a list.
encodeURIComponent is a JS function, so it would go round the #path# variable in the button code.
Hey Acoder,
The only thing i am not sure of is what type of encodeURIComponent i should use because they have 3 examples, 2 use the address an the other uses a bunch of different symbols. This is what i have, but i know it aint right. Just not sure how to go about it -
<script type="text/javascript">
-
document.write(encodeURIComponent("http://www.w3schools.com"));
-
<input type="button" name="download" value="Download" onClick="window.location.href='download.cfm?path=#path#'">
-
</script>
Thank you,
Rach
The #path# value is the address, so you need something like: - <input type="button" name="download" value="Download" onClick="window.location.href='download.cfm?path=encodeURIComponent(#path#)'">
The #path# value is the address, so you need something like: - <input type="button" name="download" value="Download" onClick="window.location.href='download.cfm?path=encodeURIComponent(#path#)'">
Hey Acoder,
So would i move this part to the top of the page -
<script type="text/javascript">
-
document.write(encodeURIComponent("http://www.w3schools.com"));
-
</script>
and leave the button like so - <input type="button" name="download" value="Download" onClick="window.location.href='download.cfm?path=encodeURIComponent(#path#)'">
an then what would i put instead of the www.w3schools.com, would i put
C:\Inetpub\Development\WWWRoot\RachelB\footprints\ form\attachments\ or
something else?
Thank you,
Rach
No, that was only an example. What you need is contained within that button. The rest you can ignore.
No, that was only an example. What you need is contained within that button. The rest you can ignore.
Hey Acoder,
so your saying i don't need this at all? - <script type="text/javascript">
-
document.write(encodeURIComponent("http://www.w3schools.com"));
-
</script>
or that i don't need the address like so - <script type="text/javascript">
-
document.write(encodeURIComponent(""));
-
</script>
-
Thank you,
Rach
No, you don't need any of that - just the button.
No, you don't need any of that - just the button.
Hey Acoder,
well the encode didn't work, when i try to download it says it cant find the file, you said something about cfparam, what would i set the cfparam to? -
<input type="button" name="download" value="Download" onClick="window.location.href='download.cfm?path=encodeURIComponent(#path#)'">
Thank you,
Rach
The cfparam is just there in case a path is not specified. You can set it to the empty string, then when outputting, if it's empty, you can just display a message instead of trying to download the file.
In download.cfm, have you changed the type attribute for the cfcontent tag? Note that you can force a download by setting the type to "application/unknown". That might be simplest solution in this case.
Post your reply Sign in to post your reply or Sign up for a free account.
Similar topics
reply
views
Thread by David Barrett |
last post: by
|
2 posts
views
Thread by Bob Greschke |
last post: by
|
1 post
views
Thread by Ray |
last post: by
|
4 posts
views
Thread by Li Weng |
last post: by
|
1 post
views
Thread by Karen Grube |
last post: by
|
16 posts
views
Thread by Philip Boonzaaier |
last post: by
|
16 posts
views
Thread by matt |
last post: by
|
3 posts
views
Thread by Mufasa |
last post: by
|
4 posts
views
Thread by Seguros Catatumbo |
last post: by
| | | | | | | | | | |