Blog

Content-Length usage

June 26, 2007

Whenever I need for Coldfusion to force a file download I use code similar to this:

<cfset cFile = "somefile.txt">
<cfset cDirectory = "C:\">

<cfheader name="Content-Disposition" value="attachment;filename=""#cFile#""">

<cfcontent type="application/unknown" file="#cDirectory##cFile#">

The problem is that I can’t see the total file size. The message varies by browser, but the situation is the same. The user can see that they have downloaded x of an unknown file size. That’s not a show stopper, but it is annoying. I’ve been looking for a solution and finally stumbled onto the simple answer:

<cFile = "somefile.txt">
<cfset cDirectory = "C:\">

<cfdirectory directory="#cDirectory#" action="LIST" name="qFile" filter="#cFile#">

<cfset nFilesize = qFile.Size>

<cfheader name="Content-Disposition" value="attachment;filename=""#cFile#""">

<cfheader name="Content-Length" value="#nFilesize#">

<cfcontent type="application/unknown" file="#cDirectory##cFile#">

0 Comments

Leave Your Comment

Your email address will not be published. Required fields are marked *


about me

An information technology professional with twenty five years experience in systems administration, computer programming, requirements gathering, customer service, and technical support.