filecoldfusiondelete-filecffile

Deleting a file in coldfusion


I have a form with the utility to attach documents and it uses a cfm file display them as below:

<cfsilent>
<cfparam name="request.ID" type="numeric" default="0">
<cfparam name="request.filename" type="string" default="">

<cfscript>  
    local = structNew();
</cfscript> 

<!--- get the request data --->
 <cfinvoke 
    component="#application.config.CFCOMPONENTS_PREFIX#com.mycompany.request_manager.responseEntity"
    method="get" 
    returnvariable="local.responses">
    <cfinvokeargument name="ID" value="#request.ID#"/>
</cfinvoke> 


<cfscript>
    local.fileName = request.filename;
    local.pathToFile = application.virtualPaths.APPROOT&"library/investigations/"&local.responses.report_number&"/"&local.fileName;
</cfscript>
</cfsilent>

<cfheader name="content-type" value="application/download">
<cfheader name="Content-Disposition" value="attachment; filename=#local.fileName#">
<cfcontent type="application/download" file="#ExpandPath(local.pathToFile)#" deletefile="No">

<cfabort> 

I need the ability to delet a file on the server, using a similar cfm file. I have a remove link which has a reference to the below page called redirect-delete.cfm. I want the files to delete on the form itself and the user staying on the form page.

<cfsilent>
<cfparam name="request.ID" type="numeric" default="0">
<cfparam name="request.filename" type="string" default="">

<cfscript>  
    local = structNew();
</cfscript> 

<!--- get the request data --->
 <cfinvoke 
    component="#application.config.CFCOMPONENTS_PREFIX#com.mycompany.request_manager.responseEntity"
    method="get" 
    returnvariable="local.responses">
    <cfinvokeargument name="ID" value="#request.ID#"/>
</cfinvoke> 


<cfscript>
    local.fileName = request.filename;
    local.pathToFile = application.virtualPaths.APPROOT&"library/investigations/"&local.responses.report_number&"/"&local.fileName;
</cfscript>
</cfsilent>

<cffile action="delete" file="#ExpandPath(local.pathToFile)#">

<cfabort> 

What should I add to the redirect-delete.cfm file for the user to get a pop-up saying are you sure you want to delete. If the user presses yes, the attachment should get deleted but the user should still stay on the form.


Solution

  • So you have a form plus a link. If the user selects the link, you want an alert to come up and if the user accepts, a file gets deleted on the server, but the user stays on the form? My approach would be:

    1. Put a 1 pixel iframe on your form page that contains the code to delete the file.
    2. For your anchor tag, have it link to javascript so that you can use the confirm popup.
    3. In the page that deletes the file, add some extra code that uses javascript to do something about the link on the form page. You can get rid of it completely, or replace it with a statement saying the file has been deleted.

    Note that step 3 will be easier if you have your link inside a <div>.