我有一个带有实用程序的表单来附加文档,它使用cfm文件显示如下:
<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>
我需要使用类似的cfm文件来删除服务器上的文件。我有一个删除链接,它引用了下面名为redirect-delete.cfm的页面。我希望文件在表单本身上删除,而用户则停留在表单页面上。
<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>
我应该在redirect-delete.cfm文件中添加什么内容,以便用户获得弹出式文件说明您确定要删除。如果用户按下是,则附件应该被删除,但用户仍应保留在表单上。
答案 0 :(得分:1)
所以你有一个表格和一个链接。如果用户选择了链接,您希望出现警报,如果用户接受,则在服务器上删除文件,但用户仍在表单上?我的方法是:
iframe
,其中包含要删除文件的代码。请注意,如果您的链接位于<div>
内,则第3步会更容易。
答案 1 :(得分:1)
如果可以,我会将这些cfm文件抽象为cfc.methods。从使用的标签,我猜你正在使用CF7左右......
在CF中,创建这样的cfc:
<cfcomponent displayName="ManageAttachment">
<cffunction name="attachFile" access="public" >
<cfargument name="ID" type="numeric" default="0">
<cfargument name="filename" type="string" default="">
<cfscript>
local = structNew();
</cfscript>
<cfinvoke
component="#application.config.CFCOMPONENTS_PREFIX#com.mycompany.request_manager.responseEntity"
method="get"
returnvariable="local.responses">
<cfinvokeargument name="ID" value="#Arguments.ID#"/>
</cfinvoke>
<cfscript>
local.fileName = Arguments.filename;
local.pathToFile = application.virtualPaths.APPROOT&"library/investigations/"&local.responses.report_number&"/"&local.fileName;
</cfscript>
<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">
<cfreturn true>
</cffunction>
<cffunction name="detachFile" access="remote" >
<cfparam name="request.ID" type="numeric" default="0">
<cfparam name="request.filename" type="string" default="">
<cfscript>
local = structNew();
</cfscript>
<cfinvoke
component="#application.config.CFCOMPONENTS_PREFIX#com.mycompany.request_manager.responseEntity"
method="get"
returnvariable="local.responses">
<cfinvokeargument name="ID" value="#Arguments.ID#"/>
</cfinvoke>
<cfscript>
local.fileName = Arguments.filename;
local.pathToFile = application.virtualPaths.APPROOT&"library/investigations/"&local.responses.report_number&"/"&local.fileName;
</cfscript>
<cffile action="delete" file="#ExpandPath(local.pathToFile)#">
<cfreturn Arguments>
</cffunction>
然后在你的显示器中你可能会做这样的事情:
<div>
<h3> Your List of Attached Files For This Report</h3>
Here is file 1. <img src='delete.png' fileName="prebuiltFileNameGoesHere1"><br />
Here is fiel 2. <img src='delete.png' fileName="prebuiltFileNameGoesHere2"><br />
</div>
然后,在js中,使用jQuery(如果不使用jQuery则使用自己的库工具):
// Assuming you use jQuery: if not, swap out with your favorite library
$(function() {
// Bind clicks on delete images
$('img.deleteLink').on('click', function(event) {
if(confirm('Are you sure want to remove this attachment?') {
detachThisFile(this.getAttribute('fileName'));
});
}
});
function detachThisFile(filename) {
$.ajax({
url : 'pathtocomponent.ManageAttachment.cfc',
cache : false,
success : function( result, textStatus, jqXHR) {
yourCallBackMethod(result);
},
data : {
method: 'detachFile', returnFormat: 'json', argumentCollection: $.toJSON({filename: filename})
}, // You might need the toJSON plug in or rewrite to be simple value
error : function(jqXHR, textStatus, errorThrown) {
console.log('ERROR OC detachThisFilenme(): ' + errorThrown);
}
});
}
function yourCallBackMethod(result) {
// Maybe you hide the link after you delete it or something to let user know it worked
// Pass an id token through to the call back as an overloaded argument so you know which element to delete
// (This is why detachFile returns arguments, so you can overload it in a useful manner)
}
我不确定您的ID来自哪里。如果您需要链接(这似乎很可能),请将其作为另一个属性添加,并像文件名一样传递...