1

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?

aparker81
  • 263
  • 1
  • 5
  • 23

1 Answers1

2

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>
Nathan Strutz
  • 8,095
  • 1
  • 37
  • 48
  • Must I append the url with the folder variable in order for the method you described to work? – aparker81 Nov 09 '12 at 19:18
  • Yes, you would need to. I hope you're doing it for yourself or an admin, and it's in a secured location, or else someone could be deleting any file they feel like out of your server. If not, you will have to think up a better method of hiding the base folder that they are allowed to manage and forcing them to stay there. – Nathan Strutz Nov 09 '12 at 19:24
  • By the way, the first method I described is easier, especially with jQuery. Call the delete page, no cflocation required, a tiny little bit of jQuery hackery and you are good to go. – Nathan Strutz Nov 09 '12 at 19:25
  • Yes, the jQuery method seems shorter and easier, but is beyond me at this time. I am changing the format of the form to look for the selected folder. Hopefully this will resolve my issue. – aparker81 Nov 09 '12 at 20:20
  • Ok, sir. provided the desired results. Thank you for the guidance. – aparker81 Nov 09 '12 at 20:49