Simultaneous Downloading of Multiple Files
At some point, you may need to download multiple files at the same time. This example
shows how to download multiple files simultaneously using
XFile.
Multi-file Download with SoftArtisans XFile
This example illustrates using
XFile'sVisual (GUI) control. You'll find this useful in getting around browsers' usual
limitation of downloading one file at a time.
For this example, we need two HTML files (SAXFileMultiDown.htm and AXFFileProgress.htm)
and one ASP file (FormResp.asp). The first HTML file will contain our ActiveX control
user interface, with which the user selects one or all the files to be downloaded.
The other one allows us to view the progress of the download. The ASP file, named
FormResp.asp, is where FileUp will take the request for the download and transfer
the files from the server to the browser.
<%@ language="vbscript" %>
<html> <head> <title>Software Artisans SA-AXFile Download Sample</title> <% '--- Server name
httpHost = "localhost"
'--- Files to be downloaded file1 = Server.URLEncode("boot.ini") file2 = Server.URLEncode("config.sys")
%> </head> <body> <p><strong><font color="black" size="5">SA-AXFile Download
Sample</font></strong></p> <p></p> <script language="javascript">
function startDownload() { winStyle="Height=300,Width=400,Status=No,Toolbar=To,_ Menubar=No,Location=No"; window.open("AXFFileProgress.htm", "_blank", winStyle); }
</script>
<object classid="CLSID:B82FA17C-F3A9-11D2-B5DD-0050041B7FF6" id="AXFFileDownload" name="AXFFileDownload" style="height: 200px; left: 0px; top: 0px; width: 400px" viewastext> </object>
<p><input id="Button1" name="Button1" type="button"
value="Start Download" onclick="startDownload()" /></p>
<script language="vbs"> Set XFile = AXFFileDownload.XFRequest
XFile.Server = <%=httpHost%>
AXFFileDownload.AddFile "C:\temp\boot.ini", _ "http://localhost/SAFileUpSamples/ClientDownload/MultiDownload" & _ "/FormResp.asp?" & "File=<%=File1%>"
AXFFileDownload.AddFile "C:\temp\config.sys", _ "http://localhost/SAFileUpSamples/ClientDownload/MultiDownload" & _ "/FormResp.asp?" & "File=<%=File2%>"
</script>
</body> </html>
|
That's obviously a lot of information to take in at once so we'll cover the code
one section at a time:
- At the top of the page, we are establishing the local ASP variables that will be
passed further into the code.
- We use "localhost" to supply the name of the web server. This information is passed
to the HttpHost variable.
- Two more variables pass the file names to be downloaded. File1 equals "boot.ini"
and File2 equals "config.sys". You will notice that we are using Server.URLEncode
to change the text into URL syntax, otherwise the file names may not pass through
correctly.
- The StartDownload function opens the download process window. We are creating an
instance of a window that will open "AXFFileProgress.htm". Inside this page is where
the process actually starts. This will be explained when we discuss this page.
- The next section includes the object tag, which will place the AXFFileDownload ActiveX
control on the form. The ID is necessary to refer to the object later on in the
code, while the other arguments, such as Appearance, BackColor, and ForeColor determine
the appearance of the control.
- The input tag creates a button whose OnClick event fires the StartDownload function
we created above.
- The last section sets some of the parameters of the AXFFileDownload control:
- We create an object called XFile, which refers to AXFFileDownload's XFRequest object.
- Using our new XFile object, we set the Server property of the XFRequest object.
In this example, we use the localhost alias for the current machine. This is supplied
by the HttpHost variable. In a real application, you could use a remote server's
name.
- The next section sets the files to be downloaded. Note that the first argument refers
to the location where the file will be saved on the user's PC. The second argument
refers to the URL of the response page and a querystring variable that ultimately
tells
FileUpwhich file to download.
AXFFileProgress.htm is a much simpler file, but the most important. This page starts
the download process. We are calling the
XFile'sProgress object, in order to display the status of the transfer. We ensure that
the progress control is displayed before the transfer starts. Otherwise, in the
multi-threaded environment of Internet Explorer, a transfer could complete before
the progress window is displayed, yielding an inconsistent user experience.
<html> <head> <title>Software Artisans SA-AXFile Download Progress</title>
<script language="javascript"> function callback() { document.getElementById("AXFFileProgress").XFRequestStream
= opener.document.getElementById("AXFFileDownload").XFRequestStream; document.getElementById("AXFFileProgress").ShowProgress(); opener.document.getElementById("AXFFileDownload").Start(); window.close(); }
</script> </head> <body onload="callback();"> <object id="AXFFileProgress"
classid="CLSID:C3EAF164-E06A-11D2-B5C9-0050041B7FF6"></object> </body> </html>
|
The sections of this file work as follows:
- The callback function sets the XFRequestStream property so that the progress control
can synchronize with the AXFFileDownload control. The Callback function also calls
the Start function of the AXFFileDownload control so that the process may begin.
The Progress window is then closed when the transaction is done.
- The body tag includes the onload parameter, which calls the callback function as
the Progress form is opened.
- The object tag creates the AXFFileProgress control to be used on the page.
The last page deals with the download via the server. This is where FileUp is used
to direct where the source file is on the server. This has very little code, but
you can imagine adding more sophisticated functions for authorizations, logging,
and management of the download.
There is the major section:
<%
'--- Get the QueryString value to be used as the source file to be downloaded. file = Request.QueryString("File")
'--- '--- Create an instance of the file download component '--- Set download = Server.CreateObject("SoftArtisans.FileUp")
'--- The root path would be supplied for the source of the download. '--- In this example it is hard coded. download.TransferFile "C:\" & File
%>
|
FileUp requires only a single parameter: the full path of the file to be transferred
to the server. In this example, both files are in the same folder. You could also
use the Querystring to specify an ID or some other means of making the server filename
hidden.
Here is an explanation of the code:
- The QueryString variable, File, is passing into a local variable with the same name.
- An instance of FileUp is created.
- The TransferFile method is used with the definition of the source directory and
the File variable, which contains the file to download.
If the download is complete, you will see checkmark over files inside of the interface.
You could also use the transfer complete method within the AXFFileProgress.htm for
additional user interaction.
Top
Copyright © 2010 SoftArtisans, Inc. All rights reserved.