Force to download a file from a FileGenie Folder List Support
Question:
How can I force a file to be downloaded from a FileGenie Folder List, instead of being opened in my browser?
Answer:
Note: Special thanks to Christopher Combs for providing the source code for the Force Download.
First create a Folder List like any other. Also apply a Folder List Repeater or a Folder List Table to show the files on your page.
Next a link will be created to a seperate page which contains the code to force a download. In this example the page that will be linked to is called filedownloader.asp.
The link that will be created is set to the Filename shown on the page.
Create a new page and save it in your website as filedownloader.asp. Copy the code below and paste it in the <body> of your new document. Save and close the document.
<%
'=======================
'Define the names of your functions
'=======================
Dim Stream
Dim Contents
Dim FileName
Dim FileExt
Const adTypeBinary = 1
'=======================
'Get the actual file name from the URL that is passed to the browser
'=======================
FileName = request.querystring("filename") 'Get the filename from the URL
Folder = request.querystring("folder") 'Get the foldername from the URL
'=======================
'GIVE AN ERROR MESSAGE IF THE URL IS EMPTY
'=======================
if FileName = "" Then
response.write "Filename Not specified."
response.end
end if
'=======================
'prevent access to certain files
'=======================
FileExt = Mid(FileName, InStrRev(FileName, ".") + 1)
select case UCase(FileExt)
Case "ASP", "ASA", "ASPX", "ASAX", "MDB"
response.write "You cannot access these file types."
response.end
end select
'=======================
'Start the download process if all is good
'=======================
response.clear
response.contentType = "application/octet-stream"
response.addheader "content-disposition", "attachment; filename=" & FileName
set stream = server.CreateObject("ADODB.Stream")
stream.type = adTypeBinary
stream.open
stream.LoadFromFile Server.MapPath("/"&Folder&"/"&FileName)
while not stream.EOS
response.BinaryWrite Stream.Read(1024 * 64)
wend
stream.Close
Set stream = Nothing
response.Flush
response.End
%>
While looking at the code you might see that two variables are being created that both get their value from the URL-paramater. To make sure these two parameters are being passed to the filedownloader.asp-page, they have to be included in the link that will be created.
Start by selecting the Filename. Enter this code in the Link field under Properties: filedownloader.asp?folder=yourfolder&filename=<%= Files.Files("Name") %>, where yourfolder should be replaced with the folder of your choice. Please note that the link contains two variables. The first one named folder is a static one (see below for other options), while the second one named filename contains the Filename from the Folder List.
The code for the Link in Code View should look like this:
<a href="filedownloader.asp?folder=yourfolder&filename=<%= Files.Files("Name") %>"><%= Files.Files("Name") %></a>
If the page would be using Dynamic Folders (FAQ: http://www.dmxzone.com/go?16783) the code would look like this:
<a href="filedownloader.asp?folder=<%=(Rs_Folders.Fields.Item("FolderName").Value)%>&filename=<%= Files.Files("Name") %>"><%= Files.Files("Name") %></a>
Save the page that contains the link and upload both pages to your server.
Note: This Force Download code can be applied to any kind of code. It is not limited to Folder Lists.
That's basically it. Preview the result on a live page and see what happens!
I'd really appreciate your feedback on your results! If you have any more questions just let me know!
Comments
Be the first to write a comment
You must me logged in to write a comment.