There are three scripts in this demo. The first lists all the files in the source directory and displays them in a form with a checkbox next to each file so they can be selected by the user. The csASPZipFile component is used to obtain the directory listing although the File System Object could be used instead.
In the downloadable example this script is called "SELECTPLUS.CFM".
<form action="finished.cfm" method="post">
<cfset dir = ExpandPath(".") & "\datafiles\">
<cfdirectory directory="#dir#" name="datafiles">
<cfset I = 0>
<cfoutput query="datafiles">
<cfif (#name# NEQ ".") and (#name# NEQ "..")>
<input type="checkbox" value="true" name="#I#">#name#<br>
</cfif>
<cfset I = I + 1>
</cfoutput>
<input type="hidden" name="directory" value="<cfoutput>#HTMLEditFormat(dir)#</cfoutput>">
<input type="reset" value="Clear"><input type="submit" value="Download">
</form>
The source directory is called "datafiles" and is immediately below the directory containing the scripts. This is specified in only one place and the value is passed to the next script as a hidden form variable. If a different directory is used it must be a full physical path and it must end with a backslash.
The second script collects the form data and uses it to create the zip file, storing it temporarily on the server. Javascript is then used to load the download script. Here are the relevant parts of this second script.
In the downloadable example this script is called "FINISHED.CFM".
<cfset dir=form["directory"]>
<cfset filename=ExpandPath(".") & "\" & CreateUUID() & ".zip">
<cfobject action="create" name="zip" class="csASPZipFileTrial.MakeZip">
<cfset I=0>
<cfdirectory directory=#dir# name="datafiles">
<cfoutput query="datafiles">
<cfparam name="form[#I#]" default="false">
<cfif form["#I#"] EQ "true">
<cfset zip.ZipAdd(dir & #name#)>
</cfif>
<cfset I = I + 1>
</cfoutput>
<cfset zip.SaveZip(#filename#)>
<cfset URL=HTMLEditFormat("makezipplus.cfm?Name=" & #filename#)>
<script language="JavaScript">
<!-- Begin
function send()
{
document.location = '<cfoutput>#Replace(URL, "\", "\\", "ALL")#</cfoutput>';
}
// End -->
</script>
<body onLoad="send();">
The zip file is constructed by adding each of the files specified in the form. As the form variables are set using checkboxes the line which reads the variables is enclosed in a <cftry> tag to avoid the error caused by an empty variable. The zip file is saved and the CreateUUID function is used to generate a temporary name. This name is passed to the download script in the URL. The Javascript onLoad event is used to call a redirection to the script that streams the zip file. It is this second script that remains displayed in the browser after the download is complete.
The third script is simple and just uses the <cfcontent> tag to download and delete the zip file. The Internet Guest User needs Modify permission in order to delete the file.
In the downloadable example this script is called "MAKEZIPPLUS.CFM".
<cfcache action="flush">
<cfcontent type="application/x-zip-compressed" deletefile="yes"
file=#url["Name"]#>
In the scripts the files are listed in the same order so the index in that list is used for identification. There are other ways of doing this such as passing the name of the file. The zip file is built by using the ZipAdd method to add each file. The full physical path of the file on the server is needed.
This example does not preserve the directory structure although the csASPZipFile component can do this. The paths on the server can be used or new paths can be specified that are not related to the location of the files on the server.
There is a simpler example which does not have the intermediate page. Click here for more.
© Chestysoft, 2024.