>Hi folks..
>
>can anyone help us out with this one, it should be so simple and I just
>can't accept all the hassle we've been having to try and figure it out.
>
>What we want to do is have the user click a link to a cfm file, that cfm
>file then returns a file (for arguements sake a .xls file) which starts to
>download to the users machine (forcing download rather than returning to
>browser).
>
>We've tried using CFCONTENT and setting the type to unknown, this will
>start the file downloading all right but the name that appears in the
>dialog boxes is the name of the orginal cfm file that was called rather
>than the name of the actual file (i.e. saves as "download.cfm" rather than
>"file2download.xls" )
>
>surely this is a simple operation? ... I really hope somebody can make me
>look stoopid with the answer..
>
>in hope
>
>.jez
>----
>Jeremy Becker ([EMAIL PROTECTED])


This thread pops up ever week or so...

There are several problems with different browsers & platforms (incompatibilities, 
partial implementations and outright bugs)

Based on help from several others, I think that the following will work on most 
current browsers.

It downloads either a text file or a TSV file which normally would be displayed.

It gets the correct name (must be supplied with the form or the link)

I have testes successfully on:

  Mac IE 5.x
  Mac NN 4.x
  Win IE 4.x
  Win IE 5.x
  Win NN 4.x

If you can test it on any other platforms, it would help.

the URL:

  http://wwww.instasales.com/Samples/DownloadFile/DownloadFile.cfm


Give it a try!

Dick

The Source:

<CFSETTING ENABLECFOUTPUTONLY="YES">

<!--- ------------------------------------------------------------ --->
<!--- StandAloneDownload.cfm                                       --->
<!--- ------------------------------------------------------------ --->
<!--- Requests a download then performs that download              --->
<!--- ------------------------------------------------------------ --->
<!--- Due to a Bug in MSIE 5.x , we must include the File Name in  --->
<!--- the Form or Link URL of the page called to invoke the        --->
<!--- download.                                                    --->
<!---                                                              --->
<!--- We do this in the form:                                      --->
<!---                                                              --->
<!---   TemplateName.cfm/FileName/                                 --->
<!---                                                              --->
<!--- For example:                                                 --->
<!---                                                              --->
<!---   <form action="StandAloneDownload.cfm/Test.txt/">           --->
<!---                                                              --->
<!--- or                                                           --->
<!---                                                              --->
<!---   <a href="StandAloneDownload.cfm/Test.txt/">                --->
<!---                                                              --->
<!--- ------------------------------------------------------------ --->
<!---                                                              --->
<!--- MSIE 4.x will try to display a file type that it recognizes, --->
<!--- in particular text and html files.  To prevent this, we      --->
<!--- trick  the browser by supplying an fake file name (without   --->
<!--- a suffix).                                                   --->
<!---                                                              --->
<!--- ------------------------------------------------------------ --->


<cfset tab   = chr(9)>
<cfset crlf  = chr(13) & chr(10)>

<!--- Get the File Path set in application.cfm --->

<cfset FilePath = Request.FilePath>

<cfif isdefined("BeginDownload")>

  <!--- ------------------------------------------------------------ --->
  <!--- Download the File                                            --->
  <!--- ------------------------------------------------------------ --->

  <cfset  RequestedFileType = ListLast(ListLast(CGI.Path_Info, "/"), ".")>

  <cfif RequestedFileType eq "tsv">

    <cfset FileName  = "Test.tsv">
    <cfset FakeName  = "Test.tsv">
    <cfset FileType  = "text/tab-separated-values">
    <cfset FakeType  = "-#FileType#-">

  <cfelse>

    <cfset FileName  = "Test.txt">
    <cfset FakeName  = "Test">
    <cfset FileType  = "text/unknown">
    <cfset FakeType  = "-#FileType#-">

  </cfif>
 
  <cfif ReFindNoCase("MSIE", CGI.HTTP_USER_AGENT)>

    <cfheader name="Content-type" value="#FileType#">
    <cfheader name="Content-Disposition" value="attachment; filename=#FakeName#" >
    <cfcontent type="#FakeType#" file="#FilePath##FileName#">

  <cfelse>

    <cfheader name="Content-type" value="#FileType#">
    <cfheader name="Content-Disposition" value="attachment; filename=#FileName#" >
    <cfcontent type="#FileType#" file="#FilePath##FileName#">

  </cfif>

<cfelse>

  <!--- ------------------------------------------------------------ --->
  <!--- Show Download Request Form                                   --->
  <!--- ------------------------------------------------------------ --->

  <cfoutput>
  <html>
  <head>
  <title>Sample - Standalone Download</title>
  </head>

  <body background="">

  <h3>Sample -  Standalone Download</h3>

  <CFSETTING ENABLECFOUTPUTONLY="YES">
 
  <cfset FileName = "Test">

  <!--- Due to a bug in MSIE, we must furnish the file name in the URL   --->
  <!--- (Form Action parameter.  This will be generated dynamically,     --->
  <!--- based on the file format selected within the form, when the form --->
  <!--- is submitted.                                                    --->

  <form name="DownloadForm" method="post"  action="">
 
    <select name="format_option">
      <option value="txt">Select File Type</option>
      <option value="txt">txt</option>
      <option value="tsv">tsv</option>
    </select>
   
    <!--- Due to a bug in MSIE, we must furnish the file name in the URL   --->

    <input type="Submit" name="BeginDownload" value="Begin Download" onClick=
        "with (document.DownloadForm) {
         var x=format_option.options[format_option.selectedIndex].value.toLowerCase();
         var y='.txt';
         if(x.indexOf('txt')>=0) {
           y='.txt'
         }else{
           if(x.indexOf('tsv'>=0)) {
             y='.tsv';
           }
         }
         action='#CGI.Script_Name#/#FileName#'+y+'/ ';
       }
      ">
  </form>

  </body>
  </html>
  </cfoutput>
 
</cfif>

<CFSETTING ENABLECFOUTPUTONLY="No">

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Structure your ColdFusion code with Fusebox. Get the official book at 
http://www.fusionauthority.com/bkinfo.cfm
FAQ: http://www.thenetprofits.co.uk/coldfusion/faq
Archives: http://www.mail-archive.com/cf-talk@houseoffusion.com/
Unsubscribe: http://www.houseoffusion.com/index.cfm?sidebar=lists

Reply via email to