Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
File 在coldfusion中删除文件_File_Coldfusion_Delete File_Cffile - Fatal编程技术网

File 在coldfusion中删除文件

File 在coldfusion中删除文件,file,coldfusion,delete-file,cffile,File,Coldfusion,Delete File,Cffile,我有一个带有附加文档实用程序的表单,它使用cfm文件显示文档,如下所示: <cfsilent> <cfparam name="request.ID" type="numeric" default="0"> <cfparam name="request.filename" type="string" default=""> <cfscript> local = structNew(); </cfscript> <!

我有一个带有附加文档实用程序的表单,它使用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> 

local=structNew();
local.fileName=request.fileName;
local.pathToFile=application.virtualPaths.APPROOT&“library/investments/”&local.responses.report\u number&“/”&local.fileName;
我需要能够删除服务器上的文件,使用类似的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> 

local=structNew();
local.fileName=request.fileName;
local.pathToFile=application.virtualPaths.APPROOT&“library/investments/”&local.responses.report\u number&“/”&local.fileName;

我应该向redirect-delete.cfm文件添加什么内容,以便用户获得一个弹出窗口,显示“您确定要删除吗?”。如果用户按yes,附件将被删除,但用户仍应保留在表单上

所以你有一个表单和一个链接。如果用户选择链接,您希望发出警报,如果用户接受,服务器上的文件将被删除,但用户仍保留在表单上?我的做法是:

  • 在包含删除文件代码的表单页面上放置一个1像素的iframe
  • 对于锚定标记,将其链接到javascript,以便使用确认弹出窗口
  • 在删除文件的页面中,添加一些额外的代码,这些代码使用javascript对表单页面上的链接进行处理。您可以完全删除该文件,也可以用声明该文件已被删除的语句替换它

  • 请注意,如果将链接放在一个

    中,步骤3会更容易。如果可以,我会将这两个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>
    

    我不知道你的身份证是从哪里来的。如果您需要它从链接(这似乎是可能的),添加它作为另一个属性,并传递它就像文件名一样

    您的
    cfsilent
    cfabort
    似乎是多余的。这是什么版本的CF?
    // 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) 
    }