Skip to: Site menu | Main content

Email Facebook LinkedIn Twitter Google

Blog...

Content-Length usage.

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:


<cfset 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#">

Comments (Comment Moderation is enabled. Your comment will not appear until approved.)
BlogCFC was created by Raymond Camden. This blog is running version 5.9.1.001.