Hi Andy,

> I have a listing of files that I want to allow a client to
> download.  When they click on the file:  I invoke a
> JavaScript function that opens a new window...

Are they text, html or otherwise not going to 'natively' invoke a
download dialog just by opening the URL?

I have several pages on my site that provide downloads just by a link
to the file (zip, wma, mp3 - you know, binary stuff, for the most
part - but also text, images and other 'difficult' file types).
Actually, it's a link through an ASP "counter" which returns the file
using an ADODB.Stream, but the basic function is the same. I think the
most important change to the returning aspect of the code is setting
the response headers in the code below, but I offer the code in it's
entirety as a solution that demonstrates how it works to force a
download via an .asp url.


'// ========================================================
<%
'[Variables]
  Dim fso, fol, fils, fil, lIter
  Dim sPage, sFile, sName, ksBrowseRoot, oStream
  ksBrowseRoot = "Q:\SharedFiles\"
  sPageTitle = "File Downloads"
  sFile = SDecode(Request.QueryString())
  sPage = Request.ServerVariables("SCRIPT_NAME")
  Set fso = Server.CreateObject("Scripting.FileSystemObject")


'[Download]
  If sFile <> "" Then
    sName = ksBrowseRoot & sFile
    If fso.FileExists( sName ) Then
      Set fil = fso.GetFile( sName )

    ' add as many headers as possible to facilitate client
      Response.Addheader _
         "Content-Disposition", _
         "attachment; filename=" & sFile 'filename
      Response.AddHeader _
         "Content-Length", fil.Size      'progress bar
      Response.ContentType  = _
         "application/x-msdownload"      'force download
      Response.CacheControl = _
         "public"                        'IE Bug (Q181228)

    ' clean up
      Set fil = Nothing
      Set fso = Nothing

    ' read binary contents
      Set oStream = Server.CreateObject("ADODB.Stream")
      oStream.Open
      oStream.Type = 1
      oStream.LoadFromFile sName

    ' write binary contents
      Response.BinaryWrite oStream.Read

    ' clean up
      oStream.Close
      Set oStream = Nothing

    ' terminate connection
      Response.End

    End If
  End If


'[Browse]
  Set fol = fso.GetFolder( ksBrowseRoot )
%>
<html>
 <head>
  <title><% =sPageTitle %></title>
 </head>
 <body>

<h2><% =sPageTitle %></h2>

<table width="100%" border="0">
  <tr bgcolor="#eeeeee">
    <td width="40%"><strong>Filename</strong></td>
    <td width="20%"><strong>File Type</strong></td>
    <td width="12%"><strong>Size</strong></td>
    <td width="28%"><strong>Modified</strong></td>
  </tr>
<%
  Set fils = fol.Files
  For Each fil in fils
    lIter = lIter + 1
%>
  <tr<%
      If (lIter - 1) Mod 4 Then
        Response.Write " bgcolor=""#eeeeee"""
      End If
    %>>
    <td><a href="<%
      Response.Write sPage & "?" _
                   & SEncode(fil.Name) _
                   & """>" & fil.Name
      %></a></td>
    <td><% =fil.Type             %></td>
    <td><% =fil.Size             %></td>
    <td><% =fil.DateLastModified %></td>
  </tr>
<%
  Next

' clean up
  Set fils = Nothing
  Set fil  = Nothing
  Set fol  = Nothing
  Set fso  = Nothing
%></table>

 </body>
</html>
<!--#include virtual="/includes/endec.asp" -->
'// ========================================================

"endec.asp" just includes the SEncode and SDecode functions that do
stuff like replacing spaces and funky characters with more useable
(and URL-friendly) characters. You could encrypt/decrypt the file
names here if you wanted to do so (in order to help reduce the
likelihood of someone that had access to the server 'guessing' file
names and attempting to exploit the server).

This method requires NO javascript or any other client-side script,
and works in every 5+ browser and IE4+ as well.

Regards,

Shawn K. Hall
http://ReliableAnswers.com/

'// ========================================================
    You know your children are growing up when they stop
    asking you where they came from and refuse to tell you
    where they're going.
      -- P. J. O'Rourke




------------------------ Yahoo! Groups Sponsor --------------------~--> 
$9.95 domain names from Yahoo!. Register anything.
http://us.click.yahoo.com/J8kdrA/y20IAA/yQLSAA/17folB/TM
--------------------------------------------------------------------~-> 

---------------------------------------------------------------------    
 Home       : http://groups.yahoo.com/group/active-server-pages
---------------------------------------------------------------------
 Post       : [EMAIL PROTECTED]
 Subscribe  : [EMAIL PROTECTED]
 Unsubscribe: [EMAIL PROTECTED]
--------------------------------------------------------------------- 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/active-server-pages/

<*> To unsubscribe from this group, send an email to:
    [EMAIL PROTECTED]

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.com/info/terms/
 

Reply via email to