调用文件删除页面后返回页面状态

时间:2012-11-09 17:50:43

标签: javascript forms coldfusion fw1

我的表单允许用户选择一个目录来查看其中包含的文件:

(files.cfm)

<form action="#buildURL('main.files')#" method="post">

        <!--- Show the subfolder path, unless already at top level --->
        <cfif subfolderPath EQ "">
            <h2>You are at the top level.</h2>
        <cfelse>
            <h2>Current Folder: #subfolderPath#</h2>
        </cfif>

        <!--- Provide a drop-down list of subfolder names --->
        Select folder:
        <select name="subfolderPath" onChange="this.form.submit()">
            <!--- Provide an option to go up one level to the parent folder, --->
            <!--- unless already at the BaseFolder --->
            <cfif listLen(subfolderPath, "/") gt 0>
                <cfset parentFolder = listDeleteAt(subfolderPath, listLen(subfolderPath, "/"), "/")>
                <option value="#parentFolder#">[parent folder]</option>
            </cfif>

            <!--- For each record in the query returned by <cfdirectory> --->
            <cfloop query="DirectoryQuery">
                <!--- If the record represents a subfolder, list it as an option --->
                <cfif Type eq "Dir">
                    <option value="#subfolderPath#/#Name#">#Name#</option>
                </cfif>
            </cfloop> 
        </select>

        <!--- Submit button to navigate to the selected folder --->
        <input type="submit" value="go">
    </form> 

显示文件时,有一个删除功能可以调用另一个页面:

<td align="absmiddle"><a href="#buildUrl('main.deleteFile?filename=#name#&folder=#rereplace(subFolderPath, '/','')#')#" onClick="alert('Are you sure you want to delete this file?')"><img src="/art/assets/images/delete.png" title="delete file" /></a></td>

在deleteFile页面(deleteFile.cfm)上,文件被删除:

<cfset local.filePath = ExpandPath( ".\upload\views\files\#rereplace(url.folder, '/','')#\" ) />
    <cffile action="delete"
            file="#local.filePath##url.filename#"
    />

然后将用户发送回上一页:

<cflocation url="#buildUrl('main.files')#" />

但不在刚刚删除文件的目录的同一视图中。如何将用户返回到文件页面并维护他所在目录的视图?

1 个答案:

答案 0 :(得分:2)

我可以想到几种方法

首先,您可以使用AJAX调用删除操作文件。成功完成后,从当前页面中删除该元素。这是一种奇特的方式,但可能很难实现,因为看起来你可能对此有点新鲜。

其次,您可以将当前页面的上下文发送到删除文件(subFolderPath变量),并在重定向时将其添加到cflocation。看起来你已经使用了url.folder变量,所以你会这样做:

<cflocation url="#buildUrl('main.files', "subfolderPath=#rc.folder")#" />

看起来你正在使用FW / 1,所以我假设'buildURL()'的工作方式与广告一样,你的网址和广告表单变量位于rc范围内。

从cflocation返回初始页面后,您需要将选择放回到您选择的文件夹中。在创建<option>标记的循环中,添加一项检查以查看是否应选择一个选项。我通常会做这样的事情:

<option value="..." <cfif listLast(rc.subFolderPath,"\") EQ name>selected</cfif>>#name#</option>