Question

My form allows users to select a directory to view the files contained within it:

(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> 

When the files are displayed, there is a delete feature that calls another page:

<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>

On the deleteFile page (deleteFile.cfm), the file is deleted:

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

The user is then sent back to the previous page:

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

But not within the same view of the directory from which the file was just deleted. How may I return the user to the files page and maintain the view of the directory he was in?

Was it helpful?

Solution

I can think of a couple methods

First, you can call the delete action file with AJAX. Once it completes successfully, remove the element from your current page. This is the fancy way, but may be difficult to pull off, as it looks like you may be somewhat new to this.

Second, you can send the context of your current page along to the delete file (the subFolderPath variable), add it to the cflocation when you redirect. It looks like you already have it with the url.folder variable, so you would do this:

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

It looks like you're using FW/1, so I assumed 'buildURL()' works as advertised, and your url & form variables are in the rc scope.

Once you arrive back on your initial page from the cflocation, you will want to put the selection back on the folder you had selected. In your loop that creates the <option> tags, add a check to see if an option should be selected. I would usually do something like this:

<option value="..." <cfif listLast(rc.subFolderPath,"\") EQ name>selected</cfif>>#name#</option>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top